此问题在此处已有答案:
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部分将不会打印包含在其中的内容。
1条答案
按热度按时间svmlkihl1#
input
的返回值是一个字符串,不能等于数字。请将条件更改为choice == "1"
和choice == "2"
。在调用
input
之后,还应该包含.strip()
,以防用户在数字之前或之后输入额外的空格。记住,要包含else
块来处理他们输入其他内容的情况。