如何使我的'秘密号码游戏'重播而不必再次执行代码?

2024-05-26 20:45:50 发布

您现在位置:Python中文网/ 问答频道 /正文

Python初学者。我一直在尝试让我的游戏在初始游戏完成后给用户一个“重玩”选项。如果我在6次尝试后猜不到数字,重放就可以工作了,但是如果我成功地猜到了数字并尝试重放,就什么都不会发生

import random

secretNumber = random.randint(1, 20)

userGuesses = 0
userInput = False

while userInput == False:

    print("Let's play a game!")
    print("I'll think of a number between 1 and 20 and you have 6 attempts to get it right.")
    print("What is your first guess?")

    while userGuesses <= 5:

        userInput = input()

        if int(userInput) > secretNumber:
            print("Too High! Try again!")
            userGuesses += 1

        elif int(userInput) < secretNumber:
            print("Too Low! Try again!")
            userGuesses += 1

        else:
            print("Congratulations! You guessed the secret number in " + str(userGuesses + 1) + " guesses!")
            print("Would you like to play again? Y or N")
            playGame = input()
            if playGame == "Y":
                userInput = False
                userGuesses = 0
            else:
                userInput = True
                print("Goodbye!")

    else:
        print("You have run out of guesses! The number I was thinking of was " + str(secretNumber) + ". Better luck "
                                                                                                     "next time!")

    print("Would you like to play again? Y or N")
    playGame = input()

    if playGame == "Y":
        userInput = False
        userGuesses = 0
    else:
        userInput = True
        print("Goodbye!")

提前谢谢你的提示


Tags: oftoyoufalsenumberinputplayif
2条回答

只要在“重新启动”游戏的地方添加一个休息时间:

import random

secretNumber = random.randint(1, 20)

userGuesses = 0
userInput = False

while userInput == False:

    print("Let's play a game!")
    print("I'll think of a number between 1 and 20 and you have 6 attempts to get it right.")
    print("What is your first guess?")

    while userGuesses <= 5:

        userInput = input()

        if int(userInput) > secretNumber:
            print("Too High! Try again!")
            userGuesses += 1

        elif int(userInput) < secretNumber:
            print("Too Low! Try again!")
            userGuesses += 1

        else:
            print("Congratulations! You guessed the secret number in " + str(userGuesses + 1) + " guesses!")
            print("Would you like to play again? Y or N")
            playGame = input()
            if playGame == "Y":
                userInput = False
                userGuesses = 0
                #Break here to exit while loop
                break
            else:
                userInput = True
                print("Goodbye!")

    else:
        print("You have run out of guesses! The number I was thinking of was " + str(secretNumber) + ". Better luck "
                                                                                                     "next time!")

    print("Would you like to play again? Y or N")
    playGame = input()

    if playGame == "Y":
        userInput = False
        userGuesses = 0
    else:
        userInput = True
        print("Goodbye!")

希望这对你有帮助

你也可以在一个单独的函数中定义你的游戏。我会这样做:

import random

def play_game():
    secretNumber = random.randint(1, 20)

    userGuesses = 0
    userInput = False

    print("Let's play a game!")
    print("I'll think of a number between 1 and 20 and you have 6 attempts to get it right.")
    print("What is your first guess?")

    while userGuesses <= 5:
        userInput = input()

        if int(userInput) > secretNumber:
            print("Too High! Try again!")
            userGuesses += 1
        elif int(userInput) < secretNumber:
            print("Too Low! Try again!")
            userGuesses += 1
        else:
            print("Congratulations! You guessed the secret number in " + str(userGuesses + 1) + " guesses!")

    print("You have run out of guesses! The number I was thinking of was " + str(secretNumber) + ". Better luck "
                                                                                                 "next time!")


if __name__ == '__main__':            
    playGame = 'y'
    while playGame == 'y':
        play_game()
        playGame = input('Would you like to play again? [y/n] ').lower()

    print("Goodbye!")

相关问题 更多 >

    热门问题