0
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.

Coder777
  • 23
  • 1
  • 2
  • 8

5 Answers5

1

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")
jackdaw
  • 564
  • 3
  • 12
0

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") 
Chris Gong
  • 8,031
  • 4
  • 30
  • 51
0

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?: ")
Junaid
  • 3,477
  • 1
  • 24
  • 24
0

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): ")
0

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.

Ada Borowa
  • 79
  • 1
  • 2