首页 > 解决方案 > Python 3 - 在猜谜游戏中随时键入“exit”退出程序

问题描述

我正在练习python挑战。我尝试制作一个简单的数字猜谜游戏,您可以随时键入退出而不是猜测退出游戏。

目前,这是我的代码所在的位置。

import random
def run():
number = random.randint(1,9)
print ("Hi, your mission is to guess the secret number. You\'ll get a clue, it\'s a number between 1 and 9 (including 1 and 9). \n Type exit whenever to exit game.")
guess = ""
guesses = 0
while guess != number:
    guess = (input("Guess a number: "))
    guesses += 1
    if int(guess) < number:
        print ("Too low...")
    elif int(guess) > number: print("Too high!")
    elif str(guess) == ("exit"):
        return "Thank you for playing!"
    else:
        print ("Good job! You guessed the secret word in {} number of tries!".format(guesses))
        run()
run()

好的!所以不起作用的部分是 "elif str(guess) == ("exit"): return "Thank you for playing!""

编辑:

import random
def run():
while True:
    number = random.randint(1,9)
    print ("Hi, your mission is to guess the secret number. You\'ll get a clue, it\'s a number between 1 and 9 (including 1 and 9). \n Type exit whenever to exit game.")
    guess = ""
    guesses = 0
    while guess != number:
        guess = (input("Guess a number: "))
        guesses += 1
        if type(guess) is int and guess < number:
            print ("Too low...")
        elif type(guess) is int and guess > number: print("Too high!")
        elif type(guess) is str and guess == ("exit"):
            return "Thank you for playing!"
        else:
            print ("Good job! You guessed the secret word in {} number of tries!".format(guesses))
            break

跑()

但这只会给我 else 语句?(“干得好!你……)”

通过重写解决并通过解决方案获得了一些帮助,并意识到整个订单被搞砸了。新的工作代码:

print("Hi, your mission is to guess the secret number. You\'ll get a clue, it\'s a number between 1 and 9 (including 1 and 9). \nType exit whenever to exit game.")
import random
number = random.randint(1, 9)
guess = 0
count = 0
    while guess != number and guess != "exit":
    guess = input("\nWhat's your guess? ")
    if guess == "exit":
        print ("Thank you for playing!")
        break
    guess = int(guess)
    count += 1
    if guess < number:
        print("Too low!")
    elif guess > number:
        print("Too high!")
    else:
        print("You got it!")
        print("And it only took you", count, "tries!")

标签: python-3.x

解决方案


当用户正确猜到一个数字时,您正在进行递归调用,因此即使用户在下一个游戏中输入“退出”,它也只会返回调用run,而不是主程序。在用户正确猜出数字后,您应该使用外部while循环来保持游戏继续进行:

import random
def run():
    while True:
        number = random.randint(1,9)
        print ("Hi, your mission is to guess the secret number. You\'ll get a clue, it\'s a number between 1 and 9 (including 1 and 9). \n Type exit whenever to exit game.")
        guess = ""
        guesses = 0
        while int(guess) != number:
            guess = (input("Guess a number: "))
            guesses += 1
            if guess == "exit":
                return "Thank you for playing!"
            elif int(guess) < number:
                print ("Too low...")
            elif int(guess) > number:
                print("Too high!")
            else:
                print ("Good job! You guessed the secret word in {} number of tries!".format(guesses))
run()

推荐阅读