0

我正在创建一个程序,要求用户输入一个 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 ""
4

2 回答 2

3
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()
于 2014-11-13T02:12:48.433 回答
0
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 循环将一遍又一遍地重复,但是当用户想要停止时,我通过设置 go 来处理它。while 循环不会执行,因为 go 现在为 false,并且您的程序将结束,因为在 while 循环之后没有其他内容可以执行。

于 2014-11-13T02:09:20.170 回答