How do you repeat a python game?

import random 
computer=random.randint(1, 100) 
guess=int(input("guess the number")) 
if guess > 100: 
    print("your guess is too high") 
elif guess < 1: 
    print("your guess is too low") 
elif guess==computer: 
    print("well done!") 
else: 
    print("you\'re wrong, guess again") 

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.

asked Oct 8, 2016 at 13:36

0

Wrap the game code to function and use while True to call it again and again

import random 

def play():
    computer=random.randint(1, 100) 
    guess=int(input("guess the number")) 
    if guess > 100: 
        print("your guess is too high") 
    elif guess < 1: 
        print("your guess is too low") 
    elif guess==computer: 
        print("well done!") 
    else: 
        print("you\'re wrong, guess again")

while True:
    answer = input("do you want to play?")
    if answer == 'yes':
        play()
    elif answer == 'no':
        break
    else:
        print("dont understand")

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.

import random 
computer=random.randint(1, 100) 
guess=int(input("guess the number\n")) 
while(guess != computer):
    if guess > 100: 
        print("your guess is too high\n") 
        guess=int(input("guess the number again\n"))
    elif guess < 1: 
        print("your guess is too low\n") 
        guess=int(input("guess the number again\n"))
if guess==computer: 
    print("well done!\n") 

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 play_again = "yes"

Place a while check on play_again:

while play_again == "yes":

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:

play_again = raw_input("\n\nWant to play again?: ")

answered Oct 8, 2016 at 13:43

How do you repeat a python game?

JunaidJunaid

2,9911 gold badge18 silver badges23 bronze badges

You can use a while loop for multiple iterations

import random 
computer=random.randint(1, 100)   
guess=int(input("guess the number"))
play_again = 'y'
while play_again == 'y':
    if guess > 100: 
        print("your guess is too high") 
    elif guess < 1: 
        print("your guess is too low") 
    elif guess==computer: 
        print("well done!") 
    else: 
        print("you\'re wrong, guess again")
    play_again = input("Want to play again(y/n): ")

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.