【问题标题】:The output from if/elif is not coming up in my code for python [duplicate]if/elif 的输出没有出现在我的 python 代码中 [重复]
【发布时间】:2021-02-16 17:52:48
【问题描述】:
rent = int("300")
shopping = int("150")
clothes = int("200")
print("you have ", + cash, "dollars")
print("You have 3 options to spend your money")
print("$300 - Rent - 1")
print("$150 - Shopping - 2")
print("$200 - Clothes - 3")
choice = input("Enter a number between 1-3 to choose how you spend your money")

if choice == 1:
    print("you now have", cash - rent, "dollars")
elif choice == 2:
    print("you now have", cash - shopping, "dollars")

if/elif 部分不会打印其中包含的内容。

【问题讨论】:

  • 那是因为choice既不等于1也不等于2。
  • 选择 = int(input())
  • int(input()) 一个不起作用

标签: python


【解决方案1】:

input的返回值为字符串,不能为数字。将条件更改为choice == "1"choice == "2"

您还应该在调用input 之后添加.strip(),以防用户在数字之前或之后输入额外的空格。并且记得包含一个else 块来处理他们输入其他内容的情况。

【讨论】:

  • 我做了选择 == "1","2" 但什么也没做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-02
  • 1970-01-01
  • 2016-05-30
  • 2017-05-19
  • 2015-01-18
  • 1970-01-01
  • 2019-05-15
相关资源
最近更新 更多