I am working on rock paper scissors game in order to learn python but my code is being greyed out/unnreachable, how would you solve it?
import random
def play():
user = input("What is your choice'r' for rock, 'p' for paper, 's' for scissors\n")
computer = random.choice(['r','p','s'])
if user==computer:
return 'tie'
def is_win(player, opponent):
return 'You won'
return 'You lost'
#FROM HERE IT IS UNREACHABLE
def is_win(player, opponent):
if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') \
or (player == 'p' and opponent == 'r'):
return True
print(play())
CodePudding user response:
Here:
def is_win(player, opponent):
if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') \
or (player == 'p' and opponent == 'r'):
return True
print(play())
You have the last line inside this if
block placed AFTER the return statement: once a function has returned a value, it exits from execution.
Also, that indentation gives place to a call loop, since play()
itself calls is_win()
.
You should put play()
- not print(play())
- with no indentation in that .py file to make the program run.
In VS Code, there are tools for improving your indentation.
Edit: I now see that you have declared is_win()
also within play()
, probably with the intent of calling it. I suggest you to look online for refreshing your Python basic skills and examples for programs of this kind.
CodePudding user response:
I tried to regenerate your code
There were three wrong places at the same place
def is_win(player, opponent):
return 'You won'
- Here it should be 1if
, not
def` - You have added extra indent to this block
- The arguments should be
user,computer
, notplayer,opponent
And the last one. I don't know whether it's a typing mistake happened while posting the question here.
print(play())
should be outside of theis_win
function