2016-05-01 27 views
-4

有沒有在if語句中使用原始輸入的方法?我想問用戶一個問題,如果他們鍵入「是」,我想要編碼在if語句後繼續工作。如果他們輸入「否」,我希望代碼說「謝謝你的時間」,然後停止在特定if語句之後的所有操作。這可能嗎?原始輸入內部If語句?

代碼(我從來沒有這樣做過,所以這是一個狂野的猜想):

tri=raw_input("Do the points that you entered form a triangle? (yes or no)") 
    tri=str(tri) 

if tri == "yes" or "Yes" or "YES": 
    print "Your triangle is an:" 
elif tri == "no" or "NO" or "No": 
    print "Thank you for your time." 
else: 
    print "Not a valid answer, please try again later." 
+0

絕對有可能的。給它一個鏡頭,併發布一些代碼,然後我們可以幫助你。 – tknickman

+0

嘗試編寫一些代碼,看看你遇到的問題 –

+0

爲什麼不試試看? –

回答

1

這給一個鏡頭:

tri = None 
while tri not in ['yes', 'no']: 
    tri=raw_input("Do the points that you entered form a triangle? (yes or no): ").lower() 

if tri == 'yes': 
    print "Your triangle is an:" 
else: 
    print "Thank you for your time."