【发布时间】:2021-02-26 04:49:07
【问题描述】:
我试图通过要求用户输入他们想要预算的金额并将其分解为特定金额并在每次迭代中从原始金额中减去该金额来计算预算 并向他们展示最后的结果
budget = int(input("Please enter the amount you have budgeted for this month: "))
print(budget)
expensess = ['Rent','Food','Car','Gym','Phone','Travel','Savings']
balance=0
budget = budget
for i in expensess:
added_balance = int(input('How much did you budget for '+str(i)))
new_balance = int(budget - added_balance)
print(new_balance)
balance += new_balance
budget = balance
print("budget is "+str(budget))
if balance is > budget:
print("You underbudgeted ")
else:
print('Good Job you would have extra money to spend'+ )
当我运行它时
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=== RESTART: C:\Users\boxfo\Desktop\Programming Challenges\Budget Analysis.py ==
Please enter the amount you have bugeted for this month: 6000
6000
How much did you budget for Rent2000
budget is 4000
How much did you budget for Food2000
budget is 6000
How much did you budget for Car3000
budget is 9000
How much did you budget for Gym
【问题讨论】:
-
提示:你写
new_balance = int(budget - added_balance),也写balance += new_balance。这意味着您从预算中减去,同时将该结果添加到预算中。 -
您能否详细说明您在上述代码中遇到的问题是什么?
-
我的代码应该计算从主要预算中减去费用后剩下的金额,但我的错误是我从主要预算中减去而不是新预算。
标签: python python-3.x loops