[英]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.