I’m making a rock, paper scissors game as a little project while I’m starting out and I have to use the same bit of code a lot. What’s the best way to do this because at the moment I have it copied and pasted all over the place. It looks untidy plus I’m having an issue with the looping while validating the input and it’s a pain to have to change it all.
user_continue = raw_input("Would you like to play again? Y/N: ")
user_continue = user_continue.upper()
#Yes or no continue for the continue while loop to continue working until correct user input.
y_n_continue = False
while y_n_continue == False:
if user_continue == "Y" or user_continue == "YES" or user_continue == "N" or user_continue == "NO":
if user_continue == "Y" or user_continue == "YES":
continue_game = True
y_n_continue = True
elif user_continue == "N" or user_continue == "NO":
continue_game = False
y_n_continue = True
else:
print "Press Y or N"
y_n_continue = False
else:
print ""
It would probably be easier if I added the whole code (With the fix, thanks to Anton.
At the moment I am getting the error – TypeError: ‘bool’ object is not callable.
I’m basically trying to get it to loop the game for as long as the user wants while also validating the inputs to make everything as bulletproof as possible.
EDIT 2 – Here is the new code and I have some test data under it.
When I launch it you are prompted to enter y/n at the start.
You also have to enter y or n twice after each game.
If you input ‘wrong’ data into the rock/paper/scissors selection it goes to the y/n selection
import random
def continue_game():
while True:
user_continue = raw_input("Would you like to play again? Y/N: ").upper()
if user_continue in ["Y", "YES", "N", "NO"]:
return user_continue in ["Y", "YES"]
else:
print "Press Y or N"
while continue_game():
#computers choice of rock, paper or scissors
computer_input = ["ROCK", "PAPER", "SCISSORS"]
computer_choice = random.choice(computer_input)
#users choice or rock, paper or scissors
user_input = raw_input("Choose rock, paper or scissors: ")
#Turns user input to upper case.
user_choice = user_input.upper()
if user_choice == "ROCK" or user_choice == "PAPER" or user_choice == "SCISSORS":
#Computer = ROCK
if computer_choice == "ROCK":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#Computer = PAPER
elif computer_choice == "PAPER":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#Computer = SCISSORS
elif computer_choice == "SCISSORS":
#user = ROCK
if user_choice == "ROCK":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You win!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = PAPER
elif user_choice == "PAPER":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You lose!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
#user = SCISSORS
elif user_choice == "SCISSORS":
print "You have chosen: " + user_choice
print "The computer has chosen: " + computer_choice
print "You draw!"
#replay?
if continue_game():
print "continue"
else:
continue_game = False
else:
print "Something has gone wrong."
else:
print "Are you sure you entered that correctly?"
Output:
Would you like to play again? Y/N: y
Choose rock, paper or scissors: rock
You have chosen: ROCK
The computer has chosen: PAPER
You lose!
Would you like to play again? Y/N: y
continue
Would you like to play again? Y/N: y
Choose rock, paper or scissors: paper
You have chosen: PAPER
The computer has chosen: ROCK
You win!
Would you like to play again? Y/N: wer
Press Y or N
Would you like to play again? Y/N: dfg
Press Y or N
Would you like to play again? Y/N: y
continue
Would you like to play again? Y/N: y
Choose rock, paper or scissors: test
Are you sure you entered that correctly?
Would you like to play again? Y/N: y
Choose rock, paper or scissors: rock
You have chosen: ROCK
The computer has chosen: SCISSORS
You win!
Would you like to play again? Y/N: n
exit
Would you like to play again? Y/N: n
>>>
I know I’m being a pain but this is all appreciated.
You can just make a function out of the code and return the value of the
continue_gamevariable. Here is a minified version of your code wrapped in a function, along with an example of its usage:UPDATE: regarding your full code, to fix the error, you need to delete the following line:
and replace:
with: