【发布时间】:2022-11-29 03:20:11
【问题描述】:
我有一个每月预算代码,显示用户是否超过/低于某个月的预算。我无法将代码分解为 def 函数。这就是我所拥有的
print("""\
This program uses a for loop to monitor your budget.
The program will prompt you to enter your budget, and amount spent
for a certain month and calculate if your were under or over budget.
You will have the option of choosing how many months you would like to
monitor.\n""")
AmountSpent = 0
Budget = 0
numMonths = int(input("Enter the number of months you would like to monitor:"))
while numMonths<0:
print("\nNegative value detected!")
numMonths = int(input("Enter the number of months you would like to monitor"))
for month in range(1,numMonths+1):
print("\n=====================================")
AmountBudgeted = float(input(f"Enter amount budgeted for month {month}:"))
while AmountBudgeted<0:
print("Negative value detected!")
AmountBudgeted = float(input(f"Enter amount budgeted for month {month}:"))
AmountSpent = float(input(f"Enter amount spent for month {month}:"))
while AmountSpent<0:
print("Negative value detected!")
AmountSpent = float(input(f"Enter amount spent for month {month}:"))
if AmountSpent <= AmountBudgeted:
underB = AmountBudgeted - AmountSpent
print(f"Good Job! You are under budget by {underB}")
else:
overB = AmountSpent - AmountBudgeted
print(f"Oops! You're over budget by {overB}")
if month == "1":
print(f'your budget is {AmountBudgeted}.')
谁能帮助我使用“def”和“Describeprogram()”和“GetMonths()”等其他函数将这段代码分解成函数?
【问题讨论】: