How do you repeat a python game?
This is my current code. it's game where the computer randomly chooses a number and the player has to guess it. i have tried but i don't know how to ask the player if they want to play again and if they say yes how to restart it. Show asked Oct 8, 2016 at 13:36
0 Wrap the game code to function and use
answered Oct 8, 2016 at 13:42
jackdawjackdaw 5643 silver badges12 bronze badges Put all the if-else statements within one big while loop that keeps looping until the user guesses the number correctly. Then, after each losing outcome, give the user another chance to guess the number so that the loop has a chance to reevaluate the guess with the next iteration. In my modification below I decided to leave the last if-else statement outside of the loop because when the user guesses correctly, the code will break out of the loop to check if the guess is correct. Of course in this scenario it has to be correct so the user is told that he or she is right and the program terminates.
answered Oct 8, 2016 at 13:41
Chris GongChris Gong 7,8464 gold badges29 silver badges48 bronze badges Just an overview of how you can do it. initialize a
variable Place a while check on play_again:
Enclose all if statements and guess input in the while loop Read the user input in a variable within but at the end of the loop:
answered Oct 8, 2016 at 13:43
JunaidJunaid 2,9911 gold badge18 silver badges23 bronze badges You can use a while loop for multiple iterations
answered Oct 8, 2016 at 13:43
You should put your code in while loop. Then after game ask user if they want to continue with input() function. If they say 'no' you can break the loop or set argument of while to False. answered Oct 8, 2016 at 13:46
Ada BorowaAda Borowa 791 silver badge2 bronze badges How do you repeat things in Python?The most common way to repeat a specific task or operation N times is by using the for loop in programming. We can iterate the code lines N times using the for loop with the range() function in Python.
How do you make a Python code repeat itself?We can loop back to the start by using a control flow statement, i.e., a while statement. To do that, wrap the complete program in a while loop that is always True. What is this? Moreover, add a continue statement at a point where you want to start the program from the beginning.
How do you repeat multiple times in Python?In Python, we utilize the asterisk operator to repeat a string. This operator is indicated by a “*” sign. This operator iterates the string n (number) of times.
|