Issue
Hello Beginner trying to learn python following this tutorial here https://www.youtube.com/watch?v=8ext9G7xspg.
I've followed along and written the code yet when I run it the program finishes instantly without Inputting into the console.
Here's my code and a screenshot of the console any help would be appreciated as I'm still learning python and I hope I can use this as a learning experience.
import random
def play():
user = input("'r' for rock, 'p' for paper, 's' for scissors")
computer = random.choice(['r', 'p', 's'])
if user == computer:
return 'Its a tie!'
# r > s, s > p, p > r
if is_win(user, computer):
return 'You won!'
return "You lost!"
def is_win(player, opponent):
# return true is player wins
# r > s, s > p, p > r
if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') \
or (player == 'p' and opponent == 'r'):
return True
Also note when I indent the code once two error's appear one at line one saying unexpected indent and another at line 21 saying that its expecting a statement however when not indented one space from the left the code isn't bugged.
Again any help would be appreciated I'm new here and I'm experiencing the same problem on another project so this could help me solve it.
Solution
You don't seem to be running the main play()
function. Do note it says "exit code 0" meaning program finished, not "line 0".
Try adding this at the bottom of the file:
if __name__ == "__main__":
play()
It will run the play()
function when the module is executed.
Keep in mind you shouldn't return 'Its a tie!'
without printing it or doing something else with the return value.
Answered By - Bharel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.