【问题标题】:?Is there a reason why my program won't perform it's if elif else statement after receiving proper input?我的程序在收到正确的输入后不会执行 if elif else 语句是否有原因
【发布时间】:2019-01-30 02:53:15
【问题描述】:

基本上,该计划的目标是听取用户对他们想要订购的商品的意见,处理商品的成本,加上销售税和小费,然后将其退回。我正在努力让我的程序接受输入并根据输入的内容运行 if elif else 语句。 我还很新,我还在想办法提出建设性的问题,所以请耐心等待。另外,我知道其中有一些未完成的部分,这可能是其中的一个因素,但我并不真正关心不完整的部分

我尝试使 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")

我希望它将“goose”作为答案并打印一个响应(最终我会让它采用分配给 goose 的数字并计算税款),但它每次都会忽略任何输入。

【问题讨论】:

  • 你的缩进被破坏了......
  • answer = input 是干什么用的?把它去掉,把上一行改成answer = input('Hon hon...) 还有answer is 'steak'不是你想的那样;将其更改为answer == 'steak',就像你对鹅所做的那样。

标签: python if-statement conditional


【解决方案1】:

input 是一个内置函数,您应该分配从输入中获得的值,但您的代码将函数本身分配给您的变量 answer

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

【讨论】:

    【解决方案2】:

    您需要将输入分配给一个变量。您的输入只是从键盘读取,您没有保存该值。解决这个问题:

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

    【讨论】:

      猜你喜欢
      • 2017-07-03
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多