How to replay code in python

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 usercomputer:
        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 usercomputer:
                    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 ""

asked Nov 13, 2014 at 1:52

1

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[]

zabop

5,7183 gold badges26 silver badges65 bronze badges

answered Nov 13, 2014 at 2:12

Alex LAlex L

8,3564 gold badges45 silver badges75 bronze badges

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 usercomputer:
            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 usercomputer:
                        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.

answered Nov 13, 2014 at 2:09

user3282276user3282276

3,4857 gold badges29 silver badges48 bronze badges

0

How do you go back a loop in Python?

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.

How do you end a program in Python?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend [freeze] the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.

Chủ Đề