I need help resolving this code. The code says that is_win is not defined.
import random
def play():
user = input("What's your choise? 'r' for rock, 'p' for paper, 's' for scissors\n")
computer = random.choice(['r', 'p', 's'])
if user == computer:
return 'It\'s a tie'
if is_win(user, computer):
return 'You won!'
return 'You lost!'
def is_win(player, opponent):
if (player == 'r' and opponent == 's') or (player == 's' and opponet == 'p') \
or (player == 'p' and opponent == 'r'):
return True
Hello,
Did you try putting the is_win function before you use it?
You cannot use a function before creating it.
You can either change the order of your code:
user = input("What's your choise? 'r' for rock, 'p' for paper, 's' for scissors\n")
computer = random.choice(['r', 'p', 's'])
def is_win(player, opponent):
if (player == 'r' and opponent == 's') or (player == 's' and opponet == 'p') \
or (player == 'p' and opponent == 'r'):
return True
if user == computer:
return 'It\'s a tie'
if is_win(user, computer):
return 'You won!'
return 'You lost!'
Or you can declare your function at the beginning of the experiment tab.