簡體   English   中英

是我的程序無法執行它的原因是收到正確輸入后的ifif else語句

[英]?Is there a reason why my program won't perform it's if elif else statement after receiving proper input

基本上,該程序的目標是從用戶那里獲得他們想要訂購的內容的輸入,處理商品的成本以及附加營業稅和小費並退還小費。 我正在努力使程序接受輸入並根據輸入內容運行if if else語句。 我還很新,我還在想辦法問一個建設性的問題,所以請在這里忍受。 另外,我知道其中有一些未完成的部分,可能會納入其中,但我並不真正擔心未完成的部分

我嘗試使if語句條件取決於使用==運算符給出的輸入,並將其更改為“ if答案是__:打印響應。我相當有信心我可以讓程序打印出來小費和稅款固定在價格上,但是到目前為止,我嘗試過的所有方法在收到任何形式的輸入后都會退出我的程序。

salesTax = 0.07 #the tax added onto the total
tip= 0.18 #the percentage for a tip
steak= 96 # a var for a steak priced so deliciously, that it *must* be good.
goose= 42 #var for the oddly familiar, yet disturbingly alien meal that is goose.
narwhal= 109 #var for a meal that questions its own existence, then laughs in the face of that question

menu = ['high-stakes steak', 'uncanny boiled goose', 'endangered carribrean narwhal caccitore']

print("Tonight's menu at Joe's ethically questionable eatery includes")
print(menu)
input('Hon hon, what\'ll it be, monsieur? the goose, stake or narwhal?')
answer = input

if answer == 'goose':
    print("Ah, very good monsieur the cost will be 42. We will beegen ze cooking of ze goose")
elif answer is 'steak':
    print("Ah, a high roller, we will begin")

我希望它以“鵝”作為答案並打印響應(最終,我將使用分配給鵝的數字來計算稅款),但它每次都忽略任何輸入。

輸入是一個內置函數,您應該分配從輸入獲取的值,但是您的代碼會將函數本身分配給變量答案

answer = input('Hon hon, what\'ll it be, monsieur? the goose, stake or narwhal?')

您需要將輸入分配給變量。 您的輸入只是從鍵盤上讀取的,您沒有保存該值。 使用以下方法解決此問題:

answer = input('Hon hon, what\'ll it be, monsieur? the goose, stake or narwhal?')

if answer == 'goose':

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM