This Python program calculates the parking fee based on the total duration of stay.
The rules are as follows:
- For up to 3 hours, the minimum fee is $5
- For each additional hour or part of an hour beyond 3 hours, an extra $0.50 is charged
- A maximum cap may apply (not specified in the prompt)
import math
def calculate_parking_fee(hours: float) -> float:
base_fee = 5.0
extra_rate = 0.5
if hours <= 3:
return base_fee
else:
extra_hours = math.ceil(hours - 3)
return base_fee + extra_hours * extra_rate
# Run the program
duration = float(input("Enter parking duration in hours: "))
fee = calculate_parking_fee(duration)
print(f"Total parking fee: ${fee:.2f}")
Total parking fee: $6.00
- The user enters the total parking duration in hours
- If the duration is 3 hours or less, the fee is $5
- If it exceeds 3 hours, each extra hour or fraction is rounded up and charged at $0.50
- The total fee is calculated and displayed with two decimal places