2017-10-05 14 views
0

我想在python中發出一個命令,我要求用戶輸入正確的哥哥年齡。如果進入23歲,該方案終止說「好工作」。否則,用戶再次被問到我兄弟的年齡。我無法理解如何製作這樣的節目。下面是我最好的猜測:python關於循環中的年齡的命令,直到出現正確的答案

age = input("what is the age of your brother? ") 
while age = 23: 
    print ("correct answer entered") 
    else: 
     print ("incorrect answer entered") 

我得到一個語法錯誤

+0

'='用於賦值,'=='用於比較。 – Barmar

+0

你需要問循環中的新時代。它應該是'年齡= 23:' – Barmar

回答

3

你要問的循環內的年齡,並結束循環時,它是正確的。由於input()返回一個字符串,所以您需要將它與一個字符串進行比較,而不是整數。要比較您需要使用的東西==,而不是=

while True: 
    age = input("what is the age of your brother? ") 
    if age == '23': 
     print("correct answer entered") 
     break 
    else: 
     print("incorrect answer entered") 
+0

非常感謝你 –

相關問題