Reputation: 153
Usual Rock Paper Scissors, but my if-else statement doesn't work. I would to appreciate if anyone could spot my errors.
def main():
#ask for input and create a list to check valid input
list = ['rock', 'paper', 'scissors']
player_A = input('Rock, paper or scissors? ').lower()
player_B = input('Rock, paper or scissors? ').lower()
if player_A not in list or player_B not in list:
print('Invalid value, please enter rock, paper or scissors')
return
else:
rule(player_A,player_B)
def rule(x,y):
if (x,y == ['rock', 'paper']) or (x,y == ['paper', 'scissors']) or (x,y == ['scissors', 'rock']):
print('{} wins'.format(y))
print('{} loses'.format(x))
elif (x,y == ['paper', 'rock']) or (x,y == 'scissors', 'paper') or (x,y == ['rock', 'scissors']):
print('{} wins'.format(x))
print('{} wins'.format(y))
else:
print('fair')
main()
Also, I would also like to know if there is a faster way to determine the win condition, seems quite clumsy to check case by case.
Upvotes: 0
Views: 396
Reputation: 1604
You can define a list of all conditions when first player wins. Then you try other way around and if both are the same then its fair.
P, S, R = 'paper', 'scissors', 'rock'
WINS = ((P, R), (R, S), (S, P))
def main():
# ask for input and create a list to check valid input
lst = [P, R, S]
player_A = input('Rock, paper or scissors? ').lower()
player_B = input('Rock, paper or scissors? ').lower()
if player_A not in lst or player_B not in lst:
print('Invalid value, please enter rock, paper or scissors')
return
else:
rule(player_A, player_B)
def rule(x, y):
if (x, y) in WINS:
print('{} wins'.format(x))
print('{} loses'.format(y))
elif (y, x) in WINS:
print('{} wins'.format(y))
print('{} loses'.format(x))
else:
print('fair')
main()
You can even make conditions checking simpler with this:
def rule(x, y):
if x == y:
print('fair')
elif (x, y) in WINS:
print('{} wins'.format(x))
print('{} loses'.format(y))
else:
print('{} wins'.format(y))
print('{} loses'.format(x))
Upvotes: 1
Reputation: 164673
I've corrected several issues / errors:
lst
instead of list
. (x, y) = ('scissors', 'paper')
The below code should work as expected:
def main():
#ask for input and create a list to check valid input
lst = ['rock', 'paper', 'scissors']
player_A = input('Rock, paper or scissors? ').lower()
player_B = input('Rock, paper or scissors? ').lower()
if player_A not in lst or player_B not in lst:
print('Invalid value, please enter rock, paper or scissors')
return
else:
rule(player_A,player_B)
def rule(x,y):
if ((x,y) == ('rock', 'paper')) or ((x,y) == ('paper', 'scissors')) or ((x,y) == ('scissors', 'rock')):
print('{} wins'.format(y))
print('{} loses'.format(x))
elif ((x,y) == ('paper', 'rock')) or ((x,y) == ('scissors', 'paper')) or ((x,y) == ('rock', 'scissors')):
print('{} wins'.format(x))
print('{} loses'.format(y))
else:
print('fair')
main()
Upvotes: 2
Reputation: 26281
Those conditions are wrong:
x, y == ['rock', 'paper']
will not result in True
or False
, but in a tuple
, with the first value being x
and the second value being False
(y == ['rock', 'paper']
).
This is because the comma has more precedence here.
To fix this, you need to lose the ambiguity. You can either:
Create an array with the values of x
and y
Compare both of them individually.
The first option is the simplest:
[x, y] == ['rock', 'paper']
The second option is not as pretty, but it's more explicit:
x == 'rock' and y == 'paper'
Upvotes: 3