python-3.x - 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!")
解决方案
当用户正确猜到一个数字时,您正在进行递归调用,因此即使用户在下一个游戏中输入“退出”,它也只会返回调用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()
推荐阅读
- c++ - 将输出重定向到文件然后返回到 C++ 中的控制台
- postgresql - 从 docker 容器运行 psql 命令
- amazon-web-services - AWS EC2 独立实例 Windows,我可以在云形成中包含启动停止计划吗?
- string - 如何使用定义的位置从 awk 编辑输出行?
- c# - .NET Core WebSocket(使用 HttpListener)通过 Apache - 我得到 404 而不是 101
- subprocess - 我想应用 python 子进程来遵循三个命令,但我没有这样做?
- sql - SQL .How to:从以 data.PIR 开头的表中选择 *
- typescript - 通用打字稿类型定义,包括。原始类型
- sql-server - 使用触发器更新具有多行的多个表
- google-bigquery - 如何在 Apache Beam 中定期从 BigQuery 读取数据?