我正在练习python挑战。在这里,我尝试制作一个简单的数字猜谜游戏,您可以随时输入exit而不是猜测来退出游戏。
此刻我的代码就在这里。
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”): 返回“谢谢您的游戏!”
编辑:
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
run()
但这只给了我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!")
答案 0 :(得分:0)
您正在对int()
的值进行str()
和guess
类型转换,然后在不首先检查实际数据类型的情况下进行比较。这绝对不是一件好事,因为一旦您输入“ exit”(或其他任何str
,float
或其他无效类型),程序将抛出ValueError
。
您可以按照以下方法检查guess
的类型,而不是进行类型转换。因为我们已经知道guess
的类型就是程序所期望的,所以这不需要类型转换。
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!"
答案 1 :(得分:0)
当用户正确猜出一个数字时,您正在进行递归调用,因此即使用户在下一个游戏中输入“退出”,它也只会返回到调用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()