Note that there are some explanatory texts on larger screens.

plurals
  1. POMost efficient way to reuse code multiple times? And also my while loops infinitly with boolean switch
    primarykey
    data
    text
    <p>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.</p> <pre><code>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 "" </code></pre> <p>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.</p> <p>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.</p> <p>EDIT 2 - Here is the new code and I have some test data under it.</p> <p>When I launch it you are prompted to enter y/n at the start. </p> <p>You also have to enter y or n twice after each game.</p> <p>If you input 'wrong' data into the rock/paper/scissors selection it goes to the y/n selection</p> <pre><code>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?" </code></pre> <p>Output:</p> <pre><code>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 &gt;&gt;&gt; </code></pre> <p>I know I'm being a pain but this is all appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload