if/elif的输出没有出现在我的python代码中[重复]

c7rzv4ha  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(80)

此问题在此处已有答案

How can I read inputs as numbers?(10个答案)
2年前关闭。

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部分将不会打印包含在其中的内容。

svmlkihl

svmlkihl1#

input的返回值是一个字符串,不能等于数字。请将条件更改为choice == "1"choice == "2"
在调用input之后,还应该包含.strip(),以防用户在数字之前或之后输入额外的空格。记住,要包含else块来处理他们输入其他内容的情况。

相关问题