def cal_parking_fee (parking_duration):
if parking_duration< 60:
fee = 2.5
else:
fee = 1.80 + 2.50
return(fee)
hours = int(input("Enter minutes "))
total_fee = cal_parking_fee(hours)
print(total_fee)
嗨,我正在尝试练习python编程,但我对这个特定问题感到困惑。它说要编写/定义一个名为cal_parking_fee
的函数,该函数需要在其中输入一个参数parking_duration
分钟,然后根据时长和停车费率退还应付费用。率是停车的第一个小时为2.50美元,此后每30分钟停车为1.80美元。我似乎无法使我的代码每30分钟就能使用一次。
答案 0 :(得分:1)
def cal_parking_fee (parking_duration):
duration_mins = parking_duration
# duration_mins = parking_duration * 60
if duration_mins < 60:
fee = 2.5
else:
duration = duration_mins - 60
fee = 2.5
while duration > 0:
duration = duration - 30
fee = fee + 1.80
return(fee)
hours = int(input("Enter hours "))
total_fee = cal_parking_fee(hours)
print(total_fee)
答案 1 :(得分:0)
这是另一种方法
def cal_parking_fee (parking_duration):
fee = 2.5
if parking_duration > 60:
# Here I subtract 2 because the 1st two 30 minutes
# have a fixed fee of 2.5 that we've already set
if (parking_duration / 30 - 2) == 0:
periods_to_pay = 1
else:
periods_to_pay = (parking_duration / 30 - 2)
fee += periods_to_pay * 1.8
return(fee)
hours = int(input("Enter minutes "))
total_fee = cal_parking_fee(hours)
print(total_fee)