简体   繁体   English

使用用户输入在 python 中循环和求和

[英]Loop and sum in python with user input

I'm trying to calculate the budget by asking the user to enter the amount they want to budget and break it down by specific amount and substract that amount from the original with every iteration and show them the last result我试图通过要求用户输入他们想要预算的金额并将其分解为特定金额并在每次迭代中从原始金额中减去该金额并显示他们最后的结果来计算预算

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'+ )    

when I run this当我运行这个

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

I've changed a bit your code.我已经改变了一点你的代码。 Just have a look:看看:

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

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

expenses_list = ["Rent", "Food", "Car", "Gym", "Phone", "Travel", "Savings"] costs_list = [“租金”、“食物”、“汽车”、“健身房”、“电话”、“旅行”、“储蓄”]

expenses = 0费用 = 0

for i in expenses_list:对于费用列表中的我:

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"
)

You should decrease the budget variable and you can calculate everything what you want in your question:你应该减少预算变量,你可以计算你想要的所有问题:

budget -= balance

I have written a working version from your code with some comments.我已经从您的代码中编写了一个带有一些注释的工作版本。

Code:代码:

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))

Tests:测试:

Underbudgeted:预算不足:

>>> 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

Good job:好工作:

>>> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM