How to repeat game of rock, paper, scissors in python
Question:
I have made a rock, paper, scissors game in python. How would I get the game to repeat over if the user enters yes
? My code appears to go into a never ending loop when the user enters anything but rock, paper, or scissors
Also I’m trying to learn when and where I should use functions. If you could show a pythonic way to separate the finished code into functions I would greatly appreciate that.
import random
a = ["rock", "paper", "scissors"]
word = input('Enter rock, paper, or scissors: ')
def game():
while True:
try:
if word not in a:
raise ValueError #this will send it to the print message and back to the input option
break
except ValueError:
print(" You must enter rock, paper, or scissors.")
comp_draw = random.choice(a)
print('The computer drew ' + comp_draw)
if comp_draw == 'rock' and word=='rock':
print('It was a tie')
elif comp_draw == 'paper' and word=='paper':
print('It was a tie')
elif comp_draw == 'scissors' and word=='scissors':
print('It was a tie')
elif comp_draw == 'paper' and word=='rock':
print('you lost')
elif comp_draw == 'rock' and word=='paper':
print('you won!')
elif comp_draw == 'rock' and word=='scissors':
print('you lost')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
game()
play_again = input('would you like to play again: ')
Answers:
Wasn’t missing much, all you needed was a loop
import random
a = ["rock", "paper", "scissors"]
word = input('Enter rock, paper, or scissors: ')
def game():
while True:
try:
if word not in a:
raise ValueError #this will send it to the print message and back to the input option
break
except ValueError:
print(" You must enter rock, paper, or scissors.")
comp_draw = random.choice(a)
print('The computer drew ' + comp_draw)
if comp_draw == 'rock' and word=='rock':
print('It was a tie')
elif comp_draw == 'paper' and word=='paper':
print('It was a tie')
elif comp_draw == 'scissors' and word=='scissors':
print('It was a tie')
elif comp_draw == 'paper' and word=='rock':
print('you lost')
elif comp_draw == 'rock' and word=='paper':
print('you won!')
elif comp_draw == 'rock' and word=='scissors':
print('you lost')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
play_again = "yes"
while play_again == "yes":
game()
play_again = input('would you like to play again: ').lower()
Instead of :
game()
play_again = input("...")
Put your game in a loop as follow :
while True:
play_again = input ("would you like to play")
if play_again == "yes" :
game()
continue
elif play_again == "no" :
break
else:
print("answer by yes or no")
continue
There was also an issue of the variable word being outside the scope of game(). It would work on the first run but not the consequent runs thereafter. This code seems to work fine and as expected.
import random
a = ["rock", "paper", "scissors"]
def game():
while True:
try:
word = input('Enter rock, paper, or scissors: ')
if word not in a:
raise ValueError # this will send it to the print message and back to the input option
break
except ValueError:
print(" You must enter rock, paper, or scissors.")
comp_draw = random.choice(a)
print('The computer drew ' + comp_draw)
if comp_draw == 'rock' and word =='rock':
print('It was a tie')
elif comp_draw == 'paper' and word =='paper':
print('It was a tie')
elif comp_draw == 'scissors' and word =='scissors':
print('It was a tie')
elif comp_draw == 'paper' and word =='rock':
print('you lost')
elif comp_draw == 'rock' and word =='paper':
print('you won!')
elif comp_draw == 'rock' and word =='scissors':
print('you lost')
elif comp_draw == 'scissors' and word =='rock':
print('you won!')
elif comp_draw == 'scissors' and word =='rock':
print('you won!')
elif comp_draw == 'scissors' and word =='rock':
print('you won!')
if __name__ == "__main__":
game()
while True:
if input('Would you like to play_again? ') == 'yes':
game()
else:
break
Put your word
variable inside your game
function. You can loop it forever. Make a playAgain
function then ask the user yes or no. If its starts with "y", restart the game()
function, if not end the program. I am using an online compiler so the module sys
wont work on my end but it should on yours.
import random
import sys
a = ["rock", "paper", "scissors"]
def playAgain():
play_again = input('would you like to play again? (y/n): ')
if play_again.startswith('y'):
game()
else:
sys.exit()
def game():
word = input('Enter rock, paper, or scissors: ')
while word not in a:
word = input('You must enter rock, paper, or scissors. Try again: ')
comp_draw = random.choice(a)
print('The computer drew ' + comp_draw)
if comp_draw == 'rock' and word=='rock':
print('It was a tie')
playAgain()
elif comp_draw == 'paper' and word=='paper':
print('It was a tie')
playAgain()
elif comp_draw == 'scissors' and word=='scissors':
print('It was a tie')
playAgain()
elif comp_draw == 'paper' and word=='rock':
print('you lost')
playAgain()
elif comp_draw == 'rock' and word=='paper':
print('you won!')
playAgain()
elif comp_draw == 'rock' and word=='scissors':
print('you lost')
playAgain()
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
playAgain()
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
playAgain()
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
playAgain()
game()
I have made a rock, paper, scissors game in python. How would I get the game to repeat over if the user enters yes
? My code appears to go into a never ending loop when the user enters anything but rock, paper, or scissors
Also I’m trying to learn when and where I should use functions. If you could show a pythonic way to separate the finished code into functions I would greatly appreciate that.
import random
a = ["rock", "paper", "scissors"]
word = input('Enter rock, paper, or scissors: ')
def game():
while True:
try:
if word not in a:
raise ValueError #this will send it to the print message and back to the input option
break
except ValueError:
print(" You must enter rock, paper, or scissors.")
comp_draw = random.choice(a)
print('The computer drew ' + comp_draw)
if comp_draw == 'rock' and word=='rock':
print('It was a tie')
elif comp_draw == 'paper' and word=='paper':
print('It was a tie')
elif comp_draw == 'scissors' and word=='scissors':
print('It was a tie')
elif comp_draw == 'paper' and word=='rock':
print('you lost')
elif comp_draw == 'rock' and word=='paper':
print('you won!')
elif comp_draw == 'rock' and word=='scissors':
print('you lost')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
game()
play_again = input('would you like to play again: ')
Wasn’t missing much, all you needed was a loop
import random
a = ["rock", "paper", "scissors"]
word = input('Enter rock, paper, or scissors: ')
def game():
while True:
try:
if word not in a:
raise ValueError #this will send it to the print message and back to the input option
break
except ValueError:
print(" You must enter rock, paper, or scissors.")
comp_draw = random.choice(a)
print('The computer drew ' + comp_draw)
if comp_draw == 'rock' and word=='rock':
print('It was a tie')
elif comp_draw == 'paper' and word=='paper':
print('It was a tie')
elif comp_draw == 'scissors' and word=='scissors':
print('It was a tie')
elif comp_draw == 'paper' and word=='rock':
print('you lost')
elif comp_draw == 'rock' and word=='paper':
print('you won!')
elif comp_draw == 'rock' and word=='scissors':
print('you lost')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
play_again = "yes"
while play_again == "yes":
game()
play_again = input('would you like to play again: ').lower()
Instead of :
game()
play_again = input("...")
Put your game in a loop as follow :
while True:
play_again = input ("would you like to play")
if play_again == "yes" :
game()
continue
elif play_again == "no" :
break
else:
print("answer by yes or no")
continue
There was also an issue of the variable word being outside the scope of game(). It would work on the first run but not the consequent runs thereafter. This code seems to work fine and as expected.
import random
a = ["rock", "paper", "scissors"]
def game():
while True:
try:
word = input('Enter rock, paper, or scissors: ')
if word not in a:
raise ValueError # this will send it to the print message and back to the input option
break
except ValueError:
print(" You must enter rock, paper, or scissors.")
comp_draw = random.choice(a)
print('The computer drew ' + comp_draw)
if comp_draw == 'rock' and word =='rock':
print('It was a tie')
elif comp_draw == 'paper' and word =='paper':
print('It was a tie')
elif comp_draw == 'scissors' and word =='scissors':
print('It was a tie')
elif comp_draw == 'paper' and word =='rock':
print('you lost')
elif comp_draw == 'rock' and word =='paper':
print('you won!')
elif comp_draw == 'rock' and word =='scissors':
print('you lost')
elif comp_draw == 'scissors' and word =='rock':
print('you won!')
elif comp_draw == 'scissors' and word =='rock':
print('you won!')
elif comp_draw == 'scissors' and word =='rock':
print('you won!')
if __name__ == "__main__":
game()
while True:
if input('Would you like to play_again? ') == 'yes':
game()
else:
break
Put your word
variable inside your game
function. You can loop it forever. Make a playAgain
function then ask the user yes or no. If its starts with "y", restart the game()
function, if not end the program. I am using an online compiler so the module sys
wont work on my end but it should on yours.
import random
import sys
a = ["rock", "paper", "scissors"]
def playAgain():
play_again = input('would you like to play again? (y/n): ')
if play_again.startswith('y'):
game()
else:
sys.exit()
def game():
word = input('Enter rock, paper, or scissors: ')
while word not in a:
word = input('You must enter rock, paper, or scissors. Try again: ')
comp_draw = random.choice(a)
print('The computer drew ' + comp_draw)
if comp_draw == 'rock' and word=='rock':
print('It was a tie')
playAgain()
elif comp_draw == 'paper' and word=='paper':
print('It was a tie')
playAgain()
elif comp_draw == 'scissors' and word=='scissors':
print('It was a tie')
playAgain()
elif comp_draw == 'paper' and word=='rock':
print('you lost')
playAgain()
elif comp_draw == 'rock' and word=='paper':
print('you won!')
playAgain()
elif comp_draw == 'rock' and word=='scissors':
print('you lost')
playAgain()
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
playAgain()
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
playAgain()
elif comp_draw == 'scissors' and word=='rock':
print('you won!')
playAgain()
game()