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)
CodePudding user response:
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)
CodePudding user response:
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.
CodePudding user response:
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")):