[英]Why won't my elif statement execute but my if statement will in Python?
[英]?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.