【问题标题】:Loop and sum in python with user input使用用户输入在python中循环和求和
【发布时间】: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


【解决方案1】:

你应该减少预算变量,你可以计算你想要的所有问题:

budget -= balance

我已经用你的代码用一些 cmets 编写了一个工作版本。

代码:

budget = int(input("Please enter the amount you have budgeted for this month: "))

expensess = ["Rent", "Food", "Car"]

for i in expensess:
    added_balance = int(input("How much did you budget for {}: ".format(i)))
    budget -= added_balance  # decrease the budget variable with the spend money
    print("Current budget is {}".format(budget))

if 0 > budget:  # If the budget is a minus number then you are underbudgeted.
    print("You underbudgeted: {}".format(abs(budget)))  # Creating ABS from negative number and print the difference.
else:
    print("Good Job you would have extra money to spend: {}".format(budget))

测试:

预算不足:

>>> python3 test.py
Please enter the amount you have budgeted for this month: 6000
How much did you budget for Rent: 2000
Current budget is 4000
How much did you budget for Food: 2000
Current budget is 2000
How much did you budget for Car: 3000
Current budget is -1000
You underbudgeted: 1000

干得好:

>>> python3 test.py
Please enter the amount you have budgeted for this month: 6000
How much did you budget for Rent: 2000
Current budget is 4000
How much did you budget for Food: 1000
Current budget is 3000
How much did you budget for Car: 1000
Current budget is 2000
Good Job you would have extra money to spend: 2000

【讨论】:

  • 如果我的回答回答了您的问题,请将其标记为“已回答”。其他 SO 用户应该看到您的问题已经得到解答! :)
【解决方案2】:

我对您的代码进行了一些更改。看看吧:

budget = int(input("Please enter the amount you have budgeted for this month: "))

print("总预算 %d " % (budget) + "\n")

expenses_list = [“租金”、“食物”、“汽车”、“健身房”、“电话”、“旅行”、“储蓄”]

费用 = 0

对于费用列表中的 i:

added_balance = int(input("How much did you budget for " + str(i) + "\t"))

# CHECK IF THE ADDED BUDGET IS GREATER THAN THE TOTAL BUDGET (budget)
if added_balance > budget:
    print("Your expense cannot be higher than your total budget!")
    exit()  # TERMINATES THE PROGRAMM OR ASK ANOTHER VALUE IF YOU WANT

expenses += added_balance

# NOW CHECK IF YOUR EXPENSES ARE ALREADY OVER THE BUDGET
if (budget - expenses) < 0:
    print(
        "YOUR EXPENSES HAVE REACHED %d" % (expenses)
        + " AND YOUR BUDGET IS %d" % (budget)
    )
    exit()  # TERMINATES THE PROGRAMM OR ASK ANOTHER VALUE IF YOU WANT

print("ADDED BUDGET: %d" % (added_balance))
print(
    "CURRENT BALANCE IS: %d " % (budget - expenses)
    + "\n ------------------------ \n"
)

【讨论】:

  • 您还应该检查用户是否告知了有效值,如下所示:budget = input("Please enter the amount you have budgeted for this month: ") if(budget.isnumeric(): 然后执行其余操作 else: print('invalid value')
  • 昨晚我最终修复了这个错误,但得出的结论与您的代码相同,但是当预算达到 0 时,我的会回到第一个条件
  • 好的,最重要的是你有这个想法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 2018-12-03
  • 2022-11-17
  • 2022-11-09
  • 1970-01-01
相关资源
最近更新 更多