所以我试着让我的Ask_号码配合我的猜谜游戏。由于某些原因,游戏代码无法确认对Ask_编号的响应是整数。我也尝试过定义响应,结果以不同的方式破坏了代码。我被这些东西弄疯了。代码如下:
import sys
def ask_number(question, low, high):
"""Ask for a number from 1 to 100."""
response = int(input(question))
while response not in range(low, high):
ask_number("Try again. ", 1, 100)
break
else:
print(response)
return response
ask_number("Give me a number from 1-100: ", 1, 100)
print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible. OR ELSE!!!\n")
le_number = ask_number
guess = int(input("Take a Friggin Guess: "))
tries = 1
# guessing loop
while guess != le_number:
if guess > le_number:
print("Lower Idgit....")
else:
print("Higher Idgit...")
if tries > 5:
print("Too bad Idgit. You tried to many times and failed... What a shocker.")
print("The number was", le_number)
input("\n\nPress the enter key to exit Idgit")
sys.exit()
guess = int(input("Take a guess:"))
tries += 1
print("You guessed it! The number was", le_number)
print("And it only took you", tries, "tries!\n")
input("\n\nPress the enter key to exit Idgit")
如果你们能帮我解释一下,那就太好了
这令人困惑:
这是:
不执行函数-您需要调用它:
最好在函数内部执行“更多”。这个
function
的功能是为您提供一个数字-所有需要做的事情都在它里面-您可以轻松地测试/使用它,并确保从中获得一个数字(除非用户杀死您的程序、死亡或计算机崩溃):输出:
这样,只有有效值才能转义函数
有关更多信息,请阅读Asking the user for input until they give a valid response上的答案
相关问题 更多 >
编程相关推荐