【问题标题】:having trouble using functions to break up my code在使用函数分解我的代码时遇到问题
【发布时间】: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()”等其他函数将这段代码分解成函数?

【问题讨论】:

    标签: python function void


    【解决方案1】:

    您可以像这样提取用户交互

    def get_nb_months():
        value = int(input("Enter the number of months you would like to monitor:"))
        while value < 0:
            print("Negative value detected!")
            value = int(input("Enter the number of months you would like to monitor"))
        return value
    

    但是随后您会注意到它们是完全相同的方法,因此您可以概括:

    def get_amount(msg, numeric_type):
        value = numeric_type(input(msg))
        while value < 0:
            print("Negative value detected!")
            value = numeric_type(input(msg))
        return value
    
    def summary(spent, budget):
        diff = abs(budget - spent)
        if spent <= budget:
            print(f"Good Job! You are under budget by {diff}")
        else:
            print(f"Oops! You're over budget by {diff}")
    
    if __name__ == "__main__":
        numMonths = get_amount("Enter the number of months you would like to monitor:", int)
        for month in range(1, numMonths + 1):
            print("
    =====================================")
            amount_budgeted = get_amount(f"Enter amount budgeted for month {month}:", float)
            amount_spent = get_amount(f"Enter amount spent for month {month}:", float)
            summary(amount_spent, amount_budgeted)
    

    【讨论】:

      猜你喜欢
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      相关资源
      最近更新 更多