2017-07-17 26 views
1

所以我是新的python 3,我只是不明白這一點。好的,所以我想問一個輸入,然後使用while循環。我要不斷地問用戶,直到他們進入integer.They不能選擇小於0或大於22,這裏的整數正確的是我:Python 3.6.1雖然陳述和要求輸入

user_input = int(input("Enter number: ") 
while (user<0) or (user>22): 
    print ("try again, ", user_input) 
    continue 

所以我只是想繼續問,直到他們輸入正確的數字。我所回的一切都沒有。請任何人都可以向我解釋這一點。我一直在研究這個小時,我不能得到它。

+0

提示:在說「再試一次」後,你需要允許用戶輸入一些東西。所以你需要再次詢問用戶輸入 – inspectorG4dget

+0

對不起,我在哪裏打印(「再試一次」,用戶)。我打算說打印(「再試一次」,user_input)。不是那樣的嗎?我用user_input再次問這個問題。 –

+0

在你的while循環中你比較'user',我相信你可能想要比較'user_input',除非它是一個錯字,否則你會因爲沒有定義'user'變量而出錯。 –

回答

0

您的問題從沒有理解如何print工作

檢查了這一點梗:

In [34]: user_input = int(input("Enter number: ")) 
Enter number: 15 

In [35]: print("try again", user_input) 
try again 15 

這就像說print("try again", 5), which prints each argument (namely, the string再試and the int 5`),用一個空格隔開。請看:

In [36]: print("try again", 5) 
try again 5 

你已經知道如何要求用戶輸入(與input功能),所以這是你會怎麼做第二次:

user_input = int(input("Enter number: ") 
while (user_input<0) or (user_input>22): 
    print ("You entered", user_input) 
    print ("That is invalid. Please try again") 
    user_input = int(input("Enter number: ") 

在另一方面,那continue在這裏是非常不相關的。你真的不需要它

+0

謝謝soooo !!!我明白出了什麼問題。 –