Codecademy Python 2 Rock, Paper, Scissors - python

I am just starting coding, growing like a baby each day
It gets tough but reviewing my notes and trying to understand/ memorize code.
Please explain for me what to do for this code.
from random import randint
"""This program will enable the user and computer to start a rock paper scissors game"""
options = ["ROCK", "PAPER", "SCISSORS"]
message = {"tie": "Yawn it's a tie!", "won": "Yay you won!", "lost": "Aww you lost!"}
def decide_winner(user_choice, computer_choice):
print("You chose %s") % (user_choice)
print("PC chose %s") % (computer_choice)
if user_choice == computer_choice:
print(message["tie"]) # tie
# user - paper , pc = rock
elif user_choice == options[1] and computer_choice == options[0]:
print(message["won"])
# user - scissor , pc = paper
elif user_choice == options[2] and computer_choice == options[1]:
print(message["won"])
# user - rock , pc - scissors
elif user_choice == options[0] and computer_choice == options[1]:
print(message["won"])
else:
print("YOU LOSE!")
def play_RPS():
user_choice = input("Enter Rock, Paper, Scissors: ")
computer_choice = options[randint(0,2)]
decide_winner(user_choice, computer_choice)
play_RPS()

A couple notes - elif user_choice == options[0] and computer_choice == options[1]: should be elif user_choice == options[0] and computer_choice == options[2]: Your comments were right but you indexed '1' instead of '2' for SCISSORS.
Python is case sensitive, so 'ROCK' will not equal 'Rock'. You need to possibly add the line
user_choice = user_choice.upper() in the function def play_RPS(): before passing it to
decide_winner(user_choice, computer_choice)

Python 2 uses input for things like variables and raw_input for things like strings.
Instead of:
user_choice = input("Enter Rock, Paper, Scissors: ")
Use
user_choice = raw_input("Enter Rock, Paper, Scissors: ")
Inputs are case sensitive
Your options are
options = ["ROCK", "PAPER", "SCISSORS"]
But if a person types "Rock" it shows that they lose
Instead change
user_choice = raw_input("Enter Rock, Paper, Scissors: ")
To
user_choice = raw_input("Enter Rock, Paper, Scissors: ").upper()

from random import randint
"""This program will enable the user and computer to start a rock paper scissors game"""
options = ["ROCK", "PAPER", "SCISSORS"]
message = {"tie": "Yawn it's a tie!", "won": "Yay you won!", "lost": "Aww you lost!"}
best_of_three = 3
def decide_winner(user_choice, computer_choice):
print('You chose %s' % user_choice)
print("PC chose %s" % computer_choice)
if user_choice == computer_choice:
print(message["tie"]) # tie
# user - paper , pc = rock
elif user_choice == options[1] and computer_choice == options[0]:
print(message["won"])
# user - scissor , pc = paper
elif user_choice == options[2] and computer_choice == options[1]:
print(message["won"])
# user - rock , pc - scissors
elif user_choice == options[0] and computer_choice == options[1]:
print(message["won"])
else:
print("YOU LOSE!")
def play_RPS():
user_choice = input("Enter Rock, Paper, Scissors: ").upper()
computer_choice = options[randint(0, 2)]
decide_winner(user_choice, computer_choice)
play_RPS()

Related

Rock, Paper, Scissors. Code won't read user input that's lowercase

I made a rock paper scissors game (code below). Is there a simpler way of making the code able to read both uppercase and lowercase inputs? So far, it only reads uppercase (i.e. Rock, Paper, Scissors). If the input is lowercase (i.e. rock, paper, scissors), the game won't tell you if you've won or not.
import random
import time
options = ["Rock", "Paper", "Scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n")
computerChoice = (random.choice(options))
print("Rock...")
time.sleep(1)
print("Paper...")
time.sleep(1)
print("Scissors...")
time.sleep(1)
print("Shoot! \n")
time.sleep(1)
print("You chose " + yourMove)
print("The computer chose " + computerChoice + "\n")
if yourMove == "Rock" and computerChoice == "Paper":
print("You lose!")
elif yourMove == "Paper" and computerChoice == "Rock":
print("You win!")
if yourMove == "Scissors" and computerChoice == "Rock":
print("You lose!")
elif yourMove == "Rock" and computerChoice == "Scissors":
print("You win!")
if yourMove == "Paper" and computerChoice == "Scissors":
print("You lose!")
elif yourMove == "Scissors" and computerChoice == "Paper":
print("You win!")
if yourMove == computerChoice:
print("It's a draw!")
I would recommend that if you are going to work with strings in a list, leave everything in lowercase.
When capturing the value desired by the user, you could use:
value = input("[Text] ").lower().strip()
Python is a case sensitive language, so
"Paper" != "paper" or " paper " != " paper"
python has many methods to work with strings.
your program can use many of them and work.
here are some solutions.
1- use lower() to make user input all lowercase and your options should be lower cased too.
options = ["rock", "paper", "scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").lower()
2- use upper() to make user input all uppercase and your options should be upper case.
options = ["ROCK", "PAPER", "SCISSORS"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").upper()
3- or you can keep your current options and just use capitalize() to make the first letter uppercase.
options = ["Rock", "Paper", "Scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").capitalize()

Not printing else when condition met

Hello I am beginner programmer in python and I am having trouble with this code. It is a rock paper scissors game I am not finished yet but it is supposed to print "I win" if the user does not pick rock when the program picks scissors. But when the program picks paper and the user picks rock it does not print "I win". I would like some help thanks. EDIT - answer was to add indent on else before "i win" thank you everybody.
import random
ask0 = input("Rock, Paper, or Scissors? ")
list0 = ["Rock", "Paper", "Scissors"]
r0 = (random.choice(list0))
print("I pick " + r0)
if ask0 == r0:
print("Tie")
elif ask0 == ("Rock"):
if r0 == ("Scissors"):
print("You Win")
else:
print("I win")
import random
ask0 = input("Rock, Paper, or Scissors? ")
list0 = ["Rock", "Paper", "Scissors"]
r0 = (random.choice(list0))
print("I pick " + r0)
if ask0 == r0:
print("Tie")
elif ask0 == ("Rock"):
if r0 == ("Scissors"):
print("You Win")
else:
print("I win")
Need to add else in Scissors condition.
Once Python as entered the elif statement, it will automatically skip the outer else. So, if you want it to print "I win" inside the elif statement, you need to indent your else statement like so:
import random
ask0 = input("Rock, Paper, or Scissors? ")
list0 = ["Rock", "Paper", "Scissors"]
r0 = (random.choice(list0))
print("I pick " + r0)
if ask0 == r0:
print("Tie")
elif ask0 == ("Rock"):
if r0 == ("Scissors"):
print("You Win")
else:
print("I win")

Python Rock Paper Scissors List Choices

Instructions: Write a program to allow the user to play Rock, Paper, Scissors against the computer. The program will be menu driven (see details below). The user can continue to play the game over and over by choosing to play the game in the menu. A game consists of a single battle: win, lose, or tie. For a single battle the user makes their move and then the computer move is displayed followed by a description of who won that battle. Do not break a tie.
Once the user chooses to quit, the program then displays the win/loss/tie record for all the games just played
The user will enter the details in the following order during gameplay:
Menu choice (1 integer, 1 to play a game, 2 to quit)
Player's move (1 string, 'Rock', 'Paper', or 'Scissors')
My problem is that I'm not sure how to make the computer choose from my list and accept it. I keep getting this...1
import random
def main():
"""
Runs a game of Rock, Paper, Scissors. Asks the user for input and plays against the computer.
"""
#print display message
print('Lets play Rock, Paper, Scissors!')
moves_list = ['Rock', 'Paper', 'Scissors']
#a place to keep track of scores
tied = 0
computer = 0
player = 0
#create a variable to control loop
play_again = 1
comp_choice = process_comp()
player_choice = process_player()
winner = determine_winner(player_choice, comp_choice)
#while loop for multiple games
while play_again == 1:
comp_choice = process_comp()
player_choice = process_player()
if comp_choice == 'Rock' or comp_choice == 'rock':
print('Computer chooses rock.')
elif comp_choice == 'Paper' or comp_choice == 'paper':
print('Computer chooses paper.')
else:
print('Computer chooses scissors.')
#call the determine winner function
winner = determine_winner(player_choice, comp_choice)
#check who won the game and add 1 to the correct winner
if winner == 'computer':
computer += 1
elif winner == 'player':
player += 1
else:
tied += 1
#ask user if they want to play again
play_again = input('Would you like to play again? (Enter 1 for yes, 2 for no): ')
#display number of games that were won
print()
print('There were', tied, 'tied games.')
print('The player won', player, 'games.')
print('The computer won', computer,'games.')
#define the computer process function
def process_comp():
#make computer choose a move from list
choosen = random.choice(moves_list)
return choosen
#make sure user enters correct option.
def process_player():
player_choice = int(input('Rock, Paper, Scissors? (Enter 2 if you want to quit):'))
#if the player enters 2, quit the game
if player_choice == 2:
print('Thank you for playing!')
play_again = 2
#check if user is inputting correct selections
while player_choice != 'Rock' or player_choice != 'rock' and player_choice != 'Paper' or player_choice != 'paper' and player_choice != 'Scissors' or player_choice != 'scissors' and player_choice != 2:
print('Error! Please enter an option that was given.')
player_choice = int(input('Rock, Paper, Scissors? (Enter 2 if you want to quit):'))
return player_choice
#define the determine winner function.
def determine_winner(player_choice, comp_choice):
#setup computer selections
if comp_choice == 'Rock' or comp_choice == 'rock':
if player_choice == 'Paper' or player_choice == 'paper':
print('You win!')
winner = 'player'
elif player_choice == 'Scissors' or player_choice == 'scissors':
print('The computer wins!')
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
if comp_choice == 'Paper' or comp_choice == 'paper':
if player_choice == 'Rock' or player_choice == 'rock':
print('The computer wins!')
winner = 'computer'
elif player_choice == 'Scissors' or player_choice == 'scissors':
print('You win!')
winner = 'player'
else:
print('The game is tied. Try again.')
winner = 'tied'
if comp_choice == 'Scissors' or comp_choice == 'scissors':
if player_choice == 'Rock' or player_choice == 'rock':
print('You win!')
winner = 'player'
elif player_choice == 'Paper' or player_choice == 'player':
print('The computer wins!')
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
return winner
main()
You are trying to translate a string, e.g. rock into an integer (number). This is not possible as Python has no way of knowing the integer you wish to translate it into. Instead you could change your process_player() to the following. I simplified your while statement, and removed the translation to int - Then you check for whether player_choice is equal to the string '2' instead of the integer version, and it should work
def process_player():
player_choice = input('Rock, Paper, Scissors? (Enter 2 if you want to quit):')
# if the player enters 2, quit the game
if player_choice == '2':
print('Thank you for playing!')
# check if user is inputting correct selections
while player_choice.lower() not in ['rock', 'paper', 'scissors', '2']:
print('Error! Please enter an option that was given.')
player_choice = input('Rock, Paper, Scissors? (Enter 2 if you want to quit):')
return player_choice

Rock, Papers and Scissors in Python

(I am new to python, please don't judge the quality of the code right now.)
So, I am making a program which let's you play rock, papers and scissors with the code. This is my code:
import random
name = str(input("Enter your name:\n"))
choices = ["rock", "papers", "scissors"]
print(f"Hi {name}, choose between rock, papers and scissors")
while True:
user_choice = str(input('> '))
user_choice.lower()
if user_choice not in choices:
print("ERROR")
computer_choice = random.choice(choices)
print(f"Computer chooses: {computer_choice}")
if user_choice == "rock":
if computer_choice == "papers":
print("Computer wins!")
elif computer_choice == "scissors":
print(f"{name} wins!")
if user_choice == "papers":
if computer_choice == "scissors":
print("Computer wins!")
if computer_choice == "rock":
print(f"{name} wins!")
if user_choice == "scissors":
if computer_choice == "rock":
print("Computer wins!")
elif computer_choice == "papers":
print(f"{name} wins!")
if user_choice == computer_choice:
print("Tie!")
But, like when I run the code, it is working out really good. However, the problem is that if the user does not enter the correct spelling, the output looks really messed up. Like this:
Enter your name:
_____
Hi _____, choose between rock, papers and scissors
> asdf
ERROR
Computer chooses: rock
I do not want the last line, the one which says "Computer chooses: rock". Please someone suggest how do I go about this.
Thanks in advance 🙏
(EDIT: Also, if you can suggest how I can include a point system in this, which calculates how many times the computer wins, the user wins and how many times the match was a tie, the effort would be really appreciated.)
Just use continue to force the user to enter a valid choice:
while True:
user_choice = str(input('> '))
user_choice = user_choice.lower()
if user_choice not in choices:
print(f"You chose {user_choice}, please choose rock, paper, or scissors")
continue
[...]
Looks like your error problem has already been solved : )
But for the tie and score counter I have prepared the following code-
import random
name = str(input("Enter your name:\n"))
choices = ["rock", "papers", "scissors"]
print(f"Hi {name}, choose between rock, papers and scissors")
Cwin = 0
Userwin = 0
Tie = 0
while True:
user_choice = str(input('> '))
user_choice.lower()
if user_choice not in choices:
print("ERROR")
computer_choice = random.choice(choices)
print(f"Computer chooses: {computer_choice}")
if user_choice == "rock":
if computer_choice == "papers":
print("Computer wins!")
Cwin = Cwin + 1
print(int(Cwin) , "Computer victories so far")
elif computer_choice == "scissors":
print(f"{name} wins!")
Userwin = Userwin + 1
print(int(Userwin) , "User victories so far")
if user_choice == "papers":
if computer_choice == "scissors":
print("Computer wins!")
Cwin = Cwin + 1
print(int(Cwin) , "Computer victories so far")
elif computer_choice == "rock":
print(f"{name} wins!")
Userwin = Userwin + 1
print(int(Userwin) , "User victories so far")
if user_choice == "scissors":
if computer_choice == "rock":
print("Computer wins!")
Cwin = Cwin + 1
print(int(Cwin) , "Computer victories so far")
elif computer_choice == "papers":
print(f"{name} wins!")
Userwin = Userwin + 1
print(int(Userwin) , "User victories so far")
if user_choice == computer_choice:
print("Tie!")
Tie = Tie + 1
print(int(Tie) , "Ties so far")
Look here the screenshot of it working properly-
Just add computer choose inside else:
import random
name = str(input("Enter your name:\n"))
choices = ["rock", "papers", "scissors"]
print(f"Hi {name}, choose between rock, papers and scissors")
while True:
user_choice = str(input('> '))
user_choice.lower()
if user_choice not in choices:
print("ERROR")
else:
computer_choice = random.choice(choices)
print(f"Computer chooses: {computer_choice}")
if user_choice == "rock":
if computer_choice == "papers":
print("Computer wins!")
elif computer_choice == "scissors":
print(f"{name} wins!")
if user_choice == "papers":
if computer_choice == "scissors":
print("Computer wins!")
if computer_choice == "rock":
print(f"{name} wins!")
if user_choice == "scissors":
if computer_choice == "rock":
print("Computer wins!")
elif computer_choice == "papers":
print(f"{name} wins!")
if user_choice == computer_choice:
print("Tie!")
Here is a possible implementation:
import random
name = str(input("Enter your name: "))
choices = ["rock", "scissors", "paper"]
print(f"Hi {name}, choose between rock, paper and scissors")
while True:
user_choice = None
while user_choice not in choices:
user_choice = str(input('> '))
user_choice.lower()
computer_choice = random.choice(choices)
print(f"Computer chooses: {computer_choice}")
if user_choice == computer_choice:
print("Tie!")
elif choices[(choices.index(user_choice) + 1) % len(choices)] == computer_choice:
print(f"{name} wins!")
else:
print(f"Computer wins!")
Some insights:
You can accomplish the comparing mechanism with a rounded list, and not explicit comparison.
Do a while loop for the user input until it is correct.

Rock, Paper And Scissors Program (Upper And Lower Case Problem)

I Was Practicing Programs Of Python And Then I Made This Rock, Paper, Scissors Program. And The Program Is Running Perfectly But The Problem Is Like This:
I Chose "Rock, Paper, Scissors" Like This Than It Runs.
But If I Enter Rock, Paper, Scissors In Any Other Way It Prints The Else Statment i.e., Invalid Choice.
I Want To Use Upper/Lower Case But How And Where.
import random
user = input("""Tell Me What Do You Choose:
~Rock
~Paper
~Scissors.
-> I Choose : """)
choose = ["Rock", "Paper", "Scissors"]
computer = random.choice(choose)
print(f"Computer Chose {computer}.")
#Function To Compare Choices
def compare(user, computer):
if user == computer:
return("It's A Tie :(....")
elif user == "Rock":
if computer == "Scissors":
return("User Wins!")
else:
return("Computer Wins!")
elif user == "Scissors":
if computer == "Paper":
return("User Wins!")
else:
return("Computer Wins!")
elif user == "Paper":
if computer == "Rock":
return("User Wins!")
else:
return("Computer Wins!")
else:
return("User Choose Out Of The Options!")
exit()
print(compare(user, computer))
just transform your input into lowercase with the lower() method
user = user.lower()
that makes sCiSsOrS into scissors
you also need to adjust your if statements to be all lowercase
elif user == "Rock": --> elif user == "rock":

Categories