[英]Why won't my input statements accept values in my program?
我正在制作一个生成随机数的程序,并要求您猜测 1-100 范围之外的数字。 输入数字后,它会根据该数字生成响应。 在这种情况下,如果输入为0
,则为Too high
、 Too low
、 Correct
或Quit too soon
,程序结束(简化,但基本相同)。
它根据您必须输入 function 的次数来计算尝试次数,并使用 while 循环不断询问数字,直到您输入正确为止。 (顺便说一句,是的,我意识到这部分是我另一个问题的副本。这是同一程序中的不同问题,所以我以同样的方式开始。)
无论如何,我对程序的最后一部分没有采取任何价值有疑问。 它应该接受keep_playing
的输入,如果它等于'y'
则继续播放。 问题是它实际上并没有使变量等于任何东西(至少我不这么认为。)所以,无论我输入什么值,它每次都会打印相同的响应。 这是代码的一小部分不起作用,尽管我觉得代码的 rest 有问题:
def keep_playing(attempts,keep_playing):
keep_playing = 'y'
if keep_playing == 'y':
guess(attempts)
keep_playing = str(input("Another game (y to continue)? "))
else:
print()
print("Thanks for playing")
return keep_playing
预期的 output 是:
Enter a number between 1 and 100, or 0 to quit: 4
Too low, try again It's 66 for testing purposes
Enter a number between 1 and 100, or 0 to quit: 67
Too high, try again It's 66 for testing purposes
Enter a number between 1 and 100, or 0 to quit: 66
Congratulations! You guessed the right number!
There were 2 attempts
Another game (y to continue)? y
Enter a number between 1 and 100, or 0 to quit: 0
You quit too early
The number was 79
Another game (y to continue)? n
Thanks for playing!
但实际的 output 是:
Enter a number between 1 and 100, or 0 to quit: 4
Too low, try again It's 66 for testing purposes
Enter a number between 1 and 100, or 0 to quit: 67
Too high, try again It's 66 for testing purposes
Enter a number between 1 and 100, or 0 to quit: 66
Congratulations! You guessed the right number!
There were 2 attempts
Another game (y to continue)? y
Enter a number between 1 and 100, or 0 to quit: 0
You quit too early
The number was 79
Another game (y to continue)? n
Another game (y to continue)? y
>>>
请注意,无论我做什么,它都会继续运行。 较高和较低的第一部分工作正常,但底部似乎坏了,我不知道如何修复它。 如果有人有任何解决方案,将不胜感激。
另外,如果有人想看到整个事情,如果有问题的话,这里是:
import random
def main():
global attempts
attempts = 0
guess(attempts)
keep_playing(attempts,keep_playing)
def guess(attempts):
number = random.randint(1,100)
print('')
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
while guess != 0:
if guess != number:
if guess < number:
print("Too low, try again It's",number, "for testing purposes") #printing the number makes it easier to fix :/
attempts += 1
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
elif guess > number:
print("Too high, try again It's",number, "for testing purposes")
attempts += 1
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
else:
print()
print("Congratulations! You guessed the right number!")
print("There were", attempts,"attempts")
print()
keep_playing = str(input("Another game (y to continue)? "))
return keep_playing
else:
print()
print("You quit too early")
print("The number was ",number)
keep_playing = str(input("Another game (y to continue)? "))
return keep_playing
def keep_playing(attempts,keep_playing):
keep_playing = 'y'
if keep_playing == 'y':
guess(attempts)
keep_playing = str(input("Another game (y to continue)? "))
else:
print()
print("Thanks for playing")
return keep_playing
main()
我注意到这里有几件事
keep_playing
是 str 变量keep_playing
而不是 function。在我下面的代码中,我将 function keep_playing
重命名为keep_playing_game
。keep_playing_game
时传入参数,以便 function 知道用户输入和尝试的内容。def keep_playing_game(attempts,keep_playing)
的第一行中设置keep_playing = 'y'
? 如果删除此行,您的程序应根据用户输入的值而不是 function 分配给keep_playing
的值按预期运行。我建议尝试这样的事情
import random
def main():
global attempts
attempts = 0
guess(attempts)
# keep_playing(attempts,keep_playing) -> this line should be removed
def guess(attempts):
number = random.randint(1,100)
print('')
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
while guess != 0:
if guess != number:
if guess < number:
print("Too low, try again It's",number, "for testing purposes") #printing the number makes it easier to fix :/
attempts += 1
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
elif guess > number:
print("Too high, try again It's",number, "for testing purposes")
attempts += 1
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
else:
print()
print("Congratulations! You guessed the right number!")
print("There were", attempts,"attempts")
print()
keep_playing = str(input("Another game (y to continue)? "))
return keep_playing_game(keep_playing, attempts)
else:
print()
print("You quit too early")
print("The number was ",number)
keep_playing = str(input("Another game (y to continue)? "))
return keep_playing_game(keep_playing, attempts)
def keep_playing_game(keep_playing, attempts):
if keep_playing == 'y':
guess(attempts)
else:
print()
print("Thanks for playing")
return
return None
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.