Python rock, paper, scissors, problem with else/elif statements - python

I am trying to make a rock, paper, scissors game, but I think I am doing something wrong with the else/elif statements.
import random
list = ['R', 'P', 'S']
def play():
x = input('Please input R, P, or S: ').upper()
y = random.choice(list)
if x == y:
print('You both picked {}, pick again'.format(x))
play()
elif (x == 'r' and y == 's') or (x == 'p' and y == 'r') or (x == 's' and y == 'p'):
print('You won! You picked {} and computer picked {}'.format(x,y))
else:
print('You lost. You picked {} and computer picked {}'.format(x,y))
play()

You have 'R', 'P', 'S' in list and 'r', 'p', 's' in your test. So == tests in your if will always be evaluated to False.
Another thing: you should avoid calling a variable list. That's a python built-in type.
Last comment (credits to #JoshuaVoskamp), your code does not handle the case where the answer is not in ['R', 'S', 'P'].
So here is a correct solution summing up the different comments made:
import random
choices = ['R', 'P', 'S']
def play():
x = input('Please input R, P, or S: ').upper()
y = random.choice(choices)
if x == y:
print('You both picked {}, pick again'.format(x))
play()
elif x + y in ['RS', 'PR', 'SP']: # clever test proposed by #user17242583
print('You won! You picked {} and computer picked {}'.format(x,y))
elif x in ['R', 'S', 'P']:
print('You lost. You picked {} and computer picked {}'.format(x,y))
else:
print('Wrong input!')
play()
play()

Try This
import tkinter as tk
from PIL import Image,ImageTk
window=tk.Tk()
window.geometry("300x500")
window.title("Rock Paper Scissors")
image=Image.open('rps.jpg')
image.thumbnail((300,300),Image.ANTIALIAS)
photo=ImageTk.PhotoImage(image)
label_image=tk.Label(image=photo)
label_image.grid(column=15,row=0)
#global variables
USER_SCORE=0
COMP_SCORE=0
USER_CHOICE=""
COMP_CHOICE=""
def choice_to_number(choice):
rps={'scissor':0,'paper':1,'rock':2}
return rps[choice]
def number_to_choice(number):
rps={0:'scissor',1:'paper',2:'rock'}
return rps[number]
def random_computer_choice():
return random.choice(['scissor','paper','rock'])
def result(human_choice,comp_choice):
global USER_SCORE
global COMP_SCORE
user=choice_to_number(human_choice)
comp=choice_to_number(comp_choice)
if(user==comp):
print("Tie")
elif((user-comp)%3==1):
print("Sorry !! Computer win")
USER_SCORE+=1
else:
print("Congarts !! You win")
COMP_SCORE+=1
#Text
text_area=tk.Text(master=window,height=12,width=30)
text_area.grid(column=15,row=4)
answer="Your Choice: {uc} \nComputer's Choice : {cc} \n Your Score : {u} \n Computer Score : {c} \n\n made by diwas pandey ".format(uc=USER_CHOICE,cc=COMP_CHOICE,u=USER_SCORE,c=COMP_SCORE, font=('arial',24,'bold'))
text_area.insert(tk.END,answer)
#Event Handling
def rock():
global USER_CHOICE
global COMP_CHOICE
USER_CHOICE='rock'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)
def paper():
global USER_CHOICE
global COMP_CHOICE
USER_CHOICE='paper'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)
def scissor():
global USER_CHOICE
global COMP_CHOICE
USER_CHOICE='scissor'
COMP_CHOICE=random_computer_choice()
result(USER_CHOICE,COMP_CHOICE)
#buttons
button1=tk.Button(text=" Scissor ",bg="red",command=scissor, height=1,width=8,font=('arial',15,'bold'))
button1.grid(column=15,row=1)
button2=tk.Button(text=" Paper ",bg="pink",command=paper, height=1,width=8,font=('arial',15,'bold'))
button2.grid(column=15,row=2)
button3=tk.Button(text=" Rock ",bg="yellow",command=rock, height=1,width=8,font=('arial',15,'bold'))
button3.grid(column=15,row=3)
window.mainloop()```

You use upper() method for the input, therefore the letter in the elif statement should be all uppercase, like:
import random
computer = ['R', 'P', 'S']
def play():
x = input('Please input R, P, or S: ').upper()
y = random.choice(computer)
if x == y:
print('You both picked {}, pick again'.format(x))
play()
elif (x == 'R' and y == 'S') or (x == 'P' and y == 'R') or (x == 'S' and y == 'P'):
print('You won! You picked {} and computer picked {}'.format(x,y))
else:
print('You lost. You picked {} and computer picked {}'.format(x,y))
play()

Related

Why is this python code not working (Rock paper sisscors)?

Why is the if statement not working correctly, please help?
Dont know what's wrong.
import random
print("Rock, Paper, Scissors")
move = input("Type your move (R for Rock, P for Paper, and S for Scissors: ")
comp_move = random.choice(["R", "S", "P"])
print("Computer: " + comp_move)
def winner(comp_move, move):
if move.upper == comp_move:
print("It's A Tie")
elif move.upper == "R" and comp_move == "S" or move.upper == "P" and comp_move == "R" \
or move.upper == "S" and comp_move == "P":
print("You Won :)")
else:
print("You Lose :(")
winner(comp_move, move)
upper is a string method, not an attribute. So 'foo'.upper is just a function, while 'foo'.upper() is a string FOO.
import random
WINNING_COMBINATIONS = {
('R', 'S'),
('P', 'R'),
('S', 'P'),
}
def winner(comp_move, move):
move = move.upper()
if move == comp_move:
print("It's A Tie")
elif (move, comp_move) in WINNING_COMBINATIONS:
print("You Won :)")
else:
print("You Lose :(")
print("Rock, Paper, Scissors")
move = input("Type your move (R for Rock, P for Paper, and S for Scissors: ")
comp_move = random.choice(["R", "S", "P"])
print("Computer: " + comp_move)
winner(comp_move, move)
this is because move.upper is a method to call, to get the uppercase value you have to call it: move.upper() is the upper value.
So, applying to your code will become:
import random
print("Rock, Paper, Scissors")
move = input("Type your move (R for Rock, P for Paper, and S for Scissors: ")
comp_move = random.choice(["R", "S", "P"])
print("Computer: " + comp_move)
def winner(comp_move, move):
if move.upper == comp_move:
print("It's A Tie")
elif move.upper() == "R" and comp_move == "S" or \
move.upper() == "P" and comp_move == "R" or \
move.upper() == "S" and comp_move == "P":
print("You Won :)")
else:
print("You Lose :(")
winner(comp_move, move)
but you shouldn't call so many times the function, it's a bad habit.
As said in a previous answer you should use .upper() instead of .upper and you should add parenthesis in your if statements to make python understanding the priority of each part, like that:
elif ((move.upper() == "R" and comp_move == "S") or (move.upper() == "P" and comp_move == "R")
or (move.upper() == "S" and comp_move == "P")):

Python - properly returning value for rock, paper, scissors

I am trying to write a Python script where the user can select a choice for 'r', 'p', or 's' and the script will generate the outcome of the Rock, Paper, Scissors game based on the returned value. This is what I have so far:
from random import choice
import sys
# Input name
meetname = input("Nice to meet you! What's your name? \n")
print(' \n')
# Game instructions
rpsthink = 'Well, ' + meetname + ", how about we play a game of Rock, Paper, Scissors?"
print(rpsthink)
print('\n')
#Ask user for choice of rock, paper, scissors
try:
user_move = str(input("Enter 'r' for rock, 'p' for paper, or 's' for scissors. The system will randomly select a choice, and the result of the game will be displayed to you. You can enter 'Q' to quit the game at any time. \n"))
except ValueError:
print("Please make sure your input is 'r', 'p', 's', or 'Q'!")
sys.exit()
print('\n')
if user_move =='Q':
print('Sorry to see you go - hope you had fun playing!')
sys.exit()
# Generate a random computer choice
def computer_rps() -> str:
#Computer will randomly select from rock, paper, scissors:
computermove: str = choice(['r','p','s'])
return computermove
def gameresult(user_move, computermove):
# Return value based on comparison of user and computer moves
# User wins
if (user_move == 'r' and computermove == 's') or (user_move == 'p' and computermove == 'r') or (user_move == 's' and computermove =='p'):
return 1
# User loses
if (user_move == 'r' and computermove == 'p') or (user_move == 's' and computermove == 'r') or (user_move == 'p' and computermove == 's'):
return -1
# Tie game
if user_move == computermove:
return 0
#Notification of game result based on returned function value
if int(gameresult(user_move, computermove)) == -1:
print("The computer made a choice of ", computermove)
print("Looks like the computer won this time...don't let that deter you - let's have another round for a shot at victory!")
if int(gameresult(user_move, computermove)) == 1:
print("The computer made a choice of ", computermove)
print('Looks like you won! Excellent choice! But how many times can you make the winning decision...?')
if int(gameresult(user_move, computermove)) == 0:
print("The computer made a choice of ", computermove)
print("Looks like it's a tie game - how about another round to settle the score?")
sys.exit()
However, I get an error of the name 'computermove' not being defined for the line if int(gameresult(user_move, computermove)) == -1. Do I need to set computermove as a global variable so that the comparison of user and computer moves can be properly done?
computermove is undefined because it's not available in the scope you're accessing it and you haven't created it anywhere else. You need to create a variable that receives the value returned by the function that generates the computer move.
computermove = computer_rps() # Add this line here and it should do it
#Notification of game result based on returned function value
if int(gameresult(user_move, computermove)) == -1:
print("The computer made a choice of ", computermove)
print("Looks like the computer won this time...don't let that deter you - let's have another round for a shot at victory!")
if int(gameresult(user_move, computermove)) == 1:
print("The computer made a choice of ", computermove)
print('Looks like you won! Excellent choice! But how many times can you make the winning decision...?')
if int(gameresult(user_move, computermove)) == 0:
print("The computer made a choice of ", computermove)
print("Looks like it's a tie game - how about another round to settle the score?")
sys.exit()
The error is pretty explicative. It's telling you that the variable computermove is not defined on the line where you are trying to use it.
You need to define said variable to the return value of the function computer_rps before calling the gameresult function.
Such as:
computermove = computer_rps()
if int(gameresult(user_move, computermove...

Rock Paper Scissors Keeping Score

I am trying to create a rock paper scissors game that keeps score but when I run this program the score resets each time. What do I need to change so that I can properly keep score?
import random
def rockPaperScissors():
playerScore = 0
computerScore = 0
print ""
p = raw_input("Type 'r' for rock, 'p' for paper or 's' for scissors: ")
choices = ['r', 'p', 's']
c = random.choice(choices)
print ""
print "Your move:", p
print "Computer's move:", c
print ""
if p == c:
print "Tie"
elif p == 'r' and c == 's':
playerScore += 1
print "You win"
elif p == 'p' and c == 'r':
playerScore += 1
print "You win"
elif p == 's' and c == 'p':
playerScore += 1
print "You win"
elif c == 'r' and p == 's':
computerScore += 1
print "You lose"
elif c == 'p' and p == 'r':
computerScore += 1
print "You lose"
elif c == 's' and p == 'p':
computerScore += 1
print "You lose"
else:
print "Try again"
print ""
print "Your score:", str(playerScore)+ ", Computer score:", str(computerScore)
while True:
rockPaperScissors()
You're calling the function in a loop. The first thing the function does is create a local variable for the score. When the function ends, that score is thrown away. It doesn't persist through multiple calls. You should return the new score and assign the new values to counters:
import random
def rockPaperScissors():
playerScore = 0
computerScore = 0
...
return playerScore, computerScore
player = 0
computer = 0
while True:
p, c = rockPaperScissors()
player += p
computer += c
print "Your score:", str(player)+ ", Computer score:", computer
Each time you run the function, you are resetting the scores. Define computerScore and playerScore outside of the function, then it will keep the values even when running the function multiple times. Use global to "import" the global variable into the function scope.
playerScore = 0
computerScore = 0
def rockPaperScissors ():
global playerScore
global computerScore
OTHER CODE

Undefined Function?

I am not sure why I am getting an error that game is not defined:
#!/usr/bin/python
# global variables
wins = 0
losses = 0
draws = 0
games = 0
# Welcome and get name of human player
print 'Welcome to Rock Paper Scissors!!'
human = raw_input('What is your name?')
print 'Hello ',human
# start game
game()
def game():
humanSelect = raw_input('Enter selection: R - Rock, P - Paper, S - Scissors, Q - Quit: ')
while humanSelect not in ['R', 'P', 'S', 'Q']:
print humanSelect, 'is not a valid selection'
humanSelect = raw_input('Enter a valid option please')
return humanSelect
main()
Because, at the time the statement game() is executed you have not yet reached the statement def game(): and game is, therefore, undefined.
If you move game() to after def game() you will then get a similar error on main() which is harder to fix as you don't appear to be defining a function called main anywhere in the code.
You have to define the function game before you can call it.
def game():
...
game()
Okay, I spent some time tinkering with this today and now have the following:
import random
import string
# global variables
global wins
wins = 0
global losses
losses = 0
global draws
draws = 0
global games
games = 0
# Welcome and get name of human player
print 'Welcome to Rock Paper Scissors!!'
human = raw_input('What is your name? ')
print 'Hello ',human
def readyToPlay():
ready = raw_input('Ready to Play? <Y> or <N> ')
ready = string.upper(ready)
if ready == 'Y':
game()
else:
if games == 0:
print 'Thanks for playing'
exit
else:
gameResults(games, wins, losses, draws)
return
def game():
global games
games += 1
human = humanChoice()
computer = computerChoice()
playResults(human, computer)
readyToPlay()
def humanChoice():
humanSelect = raw_input('Enter selection: R - Rock, P - Paper, S - Scissors: ')
while humanSelect not in ['R', 'P', 'S']:
print humanSelect, 'is not a valid selection'
humanSelect = raw_input('Enter a valid option please')
return humanSelect
def computerChoice():
computerInt = random.randint(1, 3)
if computerInt == '1':
computerSelect = 'R'
elif computerInt == '2':
computerSelect = 'P'
else:
computerSelect = 'S'
return computerSelect
def playResults(human, computer):
global draws
global wins
global losses
if human == computer:
print 'Draw'
draws += 1
elif human == 'R' and computer == 'P':
print 'My Paper wrapped your Rock, you lose.'
losses += 1
elif human == 'R' and computer == 'S':
print 'Your Rock smashed my Scissors, you win!'
wins += 1
elif human == 'P' and computer == 'S':
print 'My Scissors cut your paper, you lose.'
losses += 1
elif human == 'P' and computer == 'R':
print 'Your Paper covers my Rock, you win!'
wins += 1
elif human == 'S' and computer == 'R':
print 'My Rock smashes your Scissors, you lose.'
losses += 1
elif human == 'S' and computer == 'P':
print 'Your Scissors cut my Paper, you win!'
wins += 1
def gameResults(games, wins, losses, draws):
print 'Total games played', games
print 'Wins: ', wins, ' Losses: ',losses, ' Draws: ', draws
exit
readyToPlay()
I am going to work on the forcing the humanSelect variable to upper case in the same manner that I did with ready, ready = string.upper(ready). I ran into indentation errors earlier today, but will iron that out later tonight.
I do have a question. Is it possible to use a variable between the () of a raw_input function similar to this:
if game == 0:
greeting = 'Would you like to play Rock, Paper, Scissors?'
else:
greeting = 'Play again?'
ready = raw_input(greeting)

Global variable can't be modified in a separate function

I am trying to make a simple RPS game and can't see what I am doing wrong.
I declase pc_choise, player_choice and turn as global variables but I can't modify them in functions such as checkWinner().
If I print that value after the function has been called, it still has the initial value.
Code:
import random
import sys
pc_choices = ['r','p','s']
pc_choise = ''
player_choice = ''
turns = 0
print("\t\tWelcome to Rock Paper Scissors")
def getPcChoice():
return random.randint(1,3) - 1
def getUserChoice():
player_choice = input('Please choose: ')
turns = 1
if(player_choice.lower() not in pc_choices):
print('\nPlease use R, P, or S - *not case sensitive*\n')
getUserChoice()
else:
pc_choise = pc_choices[getPcChoice()]
print('\nYou picked ' + player_choice + ' and the PC picked ' +
pc_choise)
checkWinner()
def checkWinner():
if(player_choice.lower() == pc_choise.lower()):
print('Tie')
elif(player_choice.lower() == 'r' and pc_choise.lower() == 'p'
or player_choice.lower() == 'p' and pc_choise.lower() == 's'
or player_choice.lower() == 's' and pc_choise.lower() == 'r'):
print('You win! 😍')
else:
print('You lose! 🤯')
getUserChoice()
add the following code in the first line of the code:
global pc_choices and global pc_choice etc.
this is to mark the variable will use is a global variable.
You need to declare the variables as global within the function scope so that Python interpreter can interpret them accordingly, otherwise, they get the function scope by default. I also found indentation issues which I have fixed. Below is your modified code:
import random
import sys
pc_choices = ['r','p','s']
pc_choise = ''
player_choice = ''
turns = 0
print("\t\tWelcome to Rock Paper Scissors")
def getPcChoice():
return random.randint(1,3) - 1
def getUserChoice():
global pc_choise
global player_choice
player_choice = input('Please choose: ')
turns = 1
if(player_choice.lower() not in pc_choices):
print('\nPlease use R, P, or S - *not case sensitive*\n')
getUserChoice()
else:
pc_choise = pc_choices[getPcChoice()]
print('\nYou picked ' + player_choice + ' and the PC picked ' + pc_choise)
checkWinner()
def checkWinner():
global pc_choise
global player_choice
if(player_choice.lower() == pc_choise.lower()):
print('Tie')
elif(player_choice.lower() == 'r' and pc_choise.lower() == 'p'
or player_choice.lower() == 'p' and pc_choise.lower() == 's'
or player_choice.lower() == 's' and pc_choise.lower() == 'r'):
print('You win! 😍')
else:
print('You lose! 🤯')
getUserChoice()
However, as suggested by #mario_sunny in the comments, it's better to avoid using global variables.

Categories