How do I replay a game?

7.5k views Asked by At

Im creating a program that asks for the user to enter a number from 1-100, the program will tell the user when these numbers are too high or too low, and when they win. When they do win, they are asked if they want to play again or stop. The problem is that I don't know how to get the program to replay the game. Help is greatly appreciated (and I know that most of you will want to use def, but I don't know how to use it, so I would appreciate it if you did not use it) Thanks.

import random
count=0
user=raw_input("Welcome to Guess the Number! Please enter a number from 1-100: ")
user=int(float(user))
computer=random.randrange(0,101)
computer=int(float(computer))
while user!=computer:
    if user<computer:
        user=raw_input("This number is too low! Please try again: ")
        user=int(float(user))
        count+=1
    if user>computer:
        user=raw_input("This number is too high! Please try again: ")
        user=int(float(user))
        count+=1
else:
    count+=1
    print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " tries to get the right answer!"
    user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
    while user!="play" and user1!="stop":
        user=raw_input("Thats not what I asked for! If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
        if user=="play":
            count=0
            computer=random.randrange(0,101)
            computer=int(float(computer))
            while user!=computer:
                if user<computer:
                    user=raw_input("This number is too low! Please try again: ")
                    user=int(float(user))
                    count+=1
                if user>computer:
                    user=raw_input("This number is too high! Please try again: ")
                    user=int(float(user))
                    count+=1
            else:
                count+=1
                print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " to get the right answer!"
                user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
        if user=="stop":
            print ""
2

There are 2 answers

0
user3282276 On BEST ANSWER
import random
count=0
user=raw_input("Welcome to Guess the Number! Please enter a number from 1-100: ")

go = False

while(go is True):
    user=int(float(user))
    computer=random.randrange(0,101)
    computer=int(float(computer))
    while user!=computer:
        if user<computer:
            user=raw_input("This number is too low! Please try again: ")
            user=int(float(user))
            count+=1
        if user>computer:
            user=raw_input("This number is too high! Please try again: ")
            user=int(float(user))
            count+=1
    else:
        count+=1
        print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " tries to get the right answer!"
        user1=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
        while user!="play" and user1!="stop":
            user1=raw_input("Thats not what I asked for! If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
            if user=="play":
                count=0
                computer=random.randrange(0,101)
                computer=int(float(computer))
                while user!=computer:
                    if user<computer:
                        user=raw_input("This number is too low! Please try again: ")
                        user=int(float(user))
                        count+=1
                    if user>computer:
                        user=raw_input("This number is too high! Please try again: ")
                        user=int(float(user))
                        count+=1
                else:
                    count+=1
                    print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " to get the right answer!"
                    user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
            if user=="stop":
                #print ""
                #Change it so that you change go to False
                #The loop will not execute again
                go = False

Assuming this code works (I didn't run it) you would wrap it in some kind of loop that executes until broken out of. In this case I used a while loop that tested a boolean called go. Originally it is true, meaning that the while loop will repeat itself over and over again, but when the user wants to stop I handle that by setting go to False. The while loop won't execute because go is now false and your program will end because there is nothing else to execute after the while loop.

0
Alex L On
import random

def play_game():

    # Much nicer than int(float(random.randrange(0,101)))
    computer = random.randint(0, 101)
    count = 0

    # Keep looping until we return
    while True:
        
        count += 1
        user = int(raw_input('Please guess a number: '))

        if user < computer:
            print('too low!')
        elif user > computer:
            print('too high!')
        else:
            print('You win!')
            print('It took you {} tries to get the right answer!'.format(count)) 
            return  # Leave play_game

def main():
    
    print('Welcome!')

    while True:    
        play_game()

        play_again = raw_input('Play again? y/n: ') == 'y'
        if not play_again:
            return  # Leave main

main()