我如何重播游戏?

时间:2014-11-13 01:52:55

标签: python if-statement while-loop numbers replay

我正在创建一个程序,要求用户输入1-100之间的数字,该程序将告诉用户这些数字是太高还是太低,以及何时获胜。当他们赢了,他们被问到他们是想再次比赛还是停止比赛。问题是我不知道如何让程序重播游戏。非常感谢帮助(我知道大多数人都想使用def,但我不知道如何使用它,所以如果你不使用它我会很感激的)谢谢。

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 个答案:

答案 0 :(得分:2)

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()

答案 1 :(得分:-1)

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

假设这段代码有效(我没有运行它),你可以将它包装在某种循环中,直到它被破坏为止。在这种情况下,我使用了一个while循环来测试一个名为go的布尔值。最初它是真的,意味着while循环将一遍又一遍地重复,但是当用户想要停止时我通过设置转到False来处理它。 while循环不会执行,因为go现在为false,程序将结束,因为在while循环之后没有其他任何东西可以执行。