简体   繁体   English

如何在 python 中逐步计算罚金?

[英]How do I calculate penalty fees incrementally in python?

I am trying to calculate penalty fees incrementally, the program must take the value of the first calculation and add it to the second and give total to be printed.我正在尝试逐步计算罚款,程序必须取第一个计算的值并将其添加到第二个计算中并给出要打印的总数。

Here is my code:这是我的代码:


if days >= 1 and days <= 4:
  payment = days * 3
  print("Payment is: ", "$", payment)
  
elif days >= 5 and days <= 10:
  payment = days * 5
  print("Payment is: ", "$", payment)

My first instinct is to loop and maintain a count.我的第一直觉是循环并保持计数。

try the following solution:尝试以下解决方案:

change the range around since the values generated by range function starts from 0更改范围,因为范围 function 生成的值从 0 开始

total_penalty = 0
for i in range(total_delay):
    if i < 4:
        total_penalty += 3
    elif i >= 4 and i <= 9:
        total_penalty += 5  

print(total_penalty)

I am assuming, actual amount and default days is given as input to this function.我假设,实际金额和默认天数作为 function 的输入。

def penalty(actual_amt,days):
    penalty_amt = 0
    if days == 0:
        print('No Penalty')
    elif days >=1 and days <=5:
        penalty_amt = actual_amt + ( days * 3 )
    elif days > 5 and days <=10:
        penalty_amt = actual_amt + ( days * 5 )
   
    print(f'Your total cost with penalty is {penalty_amt}') 
    return penalty_amt       

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

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