【发布时间】:2021-03-05 12:58:28
【问题描述】:
我要做的是让初始输入接受一个数字,然后继续接受之后输入的数字,直到通过输入 0 关闭循环。输出应该是初始输入,输入的数量加起来,然后从初始数字中减去。
我想尽可能少地改变程序的整体结构。
budget = float(input('Enter amount budgeted for the month: '))
spent = 0
total = 0
while spent >= 0:
spent = float(input('Enter an amount spent(0 to quit): '))
total += spent
print ('Budgeted: $', format(budget, '.2f'))
print ('Spent: $', format(total, '.2f'))
if budget > total:
difference = budget - total
print ('You are $', format(difference, '.2f'), \
'under budget. WELL DONE!')
elif budget < total:
difference = total - budget
print ('You are $', format(difference, '.2f'), \
'over budget. PLAN BETTER NEXT TIME!')
elif budget == total:
print ('Spending matches budget. GOOD PLANNING!')
【问题讨论】:
-
你在循环中忘记了
total += spent。 -
另外,你想循环
while spent != 0- 这意味着你需要将spent初始化为0以外的东西。 -
@JohnnyMopp 我确实有“总 += 花费”,我的错误是我复制了错误的窗口。我试图将初始值从 0 更改为它只是将其全部破坏。感谢您指出缺少的部分!
-
您的编辑发生了很大变化...我会输入答案。
标签: python while-loop calculator operands