簡體   English   中英

使用用戶輸入在 python 中循環和求和

[英]Loop and sum in python with user input

我試圖通過要求用戶輸入他們想要預算的金額並將其分解為特定金額並在每次迭代中從原始金額中減去該金額並顯示他們最后的結果來計算預算

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

我已經改變了一點你的代碼。 看看:

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

print("總預算 %d " % (預算) + "\n")

costs_list = [“租金”、“食物”、“汽車”、“健身房”、“電話”、“旅行”、“儲蓄”]

費用 = 0

對於費用列表中的我:

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

我已經從您的代碼中編寫了一個帶有一些注釋的工作版本。

代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM