【发布时间】:2022-11-14 11:44:36
【问题描述】:
我正在运行这个 for 循环代码,它正在创建一个错误,我找不到它的问题
print("""\
This program will prompt you to enter your budget, and amount spent
for a certain month and calculate if your were under or over budget.
You will have the option of choosing how many months you would like to
monitor.\n""")
AmountSpent = 0
Budget = 0
numMonths = int(input("Enter the number of months you would like to monitor:"))
while numMonths<0:
print("\nNegative value detected!")
numMonths = int(input("Enter the number of months you would like to monitor"))
for month in range(1, numMonths+1):
print("\n=====================================")
AmountBudgeted = float(input("Enter amount budgeted for month "+month+":"))
while AmountBudgeted<0:
print("Negative value detected!")
AmountBudgeted = float(input("Enter amount budgeted for month "+month+":"))
AmountSpent = float(input("Enter amount spent for month "+month+":"))
while AmountSpent<0:
print("Negative value detected!")
AmountSpent = float(input("Enter amount spent for month "+month+":"))
if AmountSpent <= AmountBudgeted:
underBy = AmountBudgeted - AmountSpent
print("Under budget by " + underBy)
else:
overBy = AmountSpent - AmountBudgeted
print("Over budget by " + overBy)
if month == "1":
print(f'your budget is {AmountBudgeted}.')
关于为什么我会收到此错误的任何想法?我试图自己弄清楚,但我不知道为什么它是错的
【问题讨论】:
-
错误代码是第 14 行,在 <module> AmountBudgeted = float(input("Enter amount budgeted for month "+month+":")) TypeError: can only concatenate str (not "int") to str
标签: python for-loop while-loop