0

The questions asks us to create a Python program that asks the user to input the number of cars, the parking duration and parking fee and calculate its total fees and total average. I'm able to find everything I need EXCEPT the total average.

sum = 0.00
fee = 0
avg = 0

x = int(input("Number of cars:"))

while x > 0:
    y = float(input("Duration of parking:"))
    if y <= 3:
        fee = 2
        print("Fee: 2")
    elif 3 < y <= 23
        fee = 2 + (0.05 * y)
    elif y == 24:
        fee = 10
        print("Fee: 10")
    x = x - 1
    sum = sum + fee
    avg = sum / x
print("Total fees:", sum)
print("Average:")

This is my code and the only problem is the average. It gives me a 'ZeroDivisionError: float division zero' for my 'avg = sum / x' so it's basically saying my x is zero and I have no idea why or how to solve it. I've tried almost everything. Please help.

3
  • Move line x=x-1 to after you calculate the avg. Commented Jan 26, 2021 at 14:14
  • Have a variable store a copy of x before the loop and use that to compute average. Commented Jan 26, 2021 at 14:15
  • 1. Please don't override the sum keyword. 2. Please use a dictionary for the fees or somethings else, but not a super long if...else, it's bad code. Commented Jan 26, 2021 at 14:16

3 Answers 3

1

Your algorithm does the division by the number of cars, x, but the same variable is used for decrementation.

Proposal :

  • request number of car and store it to another variable,
  • do the average computation only at the very end, once all fees have been computed.

e.g. :

n_cars = x = int(input("how many cars?")

while x > 0:
    # more stuff
    ...
    sum += fee

...
avg = sum / n_cars
...
Sign up to request clarification or add additional context in comments.

2 Comments

Won't work, x is being changed each iteration!
indeed! I missed the algorithm error. I updated my answer.
1

Here is a simpler working implementation, with shorter and more clear code:

import math

MIN_FEE = 2.0
MAX_FEE = 10

x = int(input("How many cars?: "))
cost = 0

for _ in range(x):
    y = float(input("Parking duration for car: "))
    fee = min(math.ceil(max(MIN_FEE, (y + 1) * 0.5)), MAX_FEE)
    print(f"Parking fee (RM): {fee}")
    cost += fee

print("Total Fee (RM):", cost)
print("Average (RM):", cost / x)

Note that you usually don't need a copy paste of the same block too many times, it's unreadable.

Comments

0

The problem is you are decrementing x at each iteration and it becomes 0 eventually and hence you get the error.

Either use for loop to iterate for n inputs or use two objects, one for iterating and other for computing the average.

Also, you need to compute the average outside the while loop.

Try this:

sum = 0.00
fee = 0
avg = 0

x = int(input("How many cars?: "))
z = x

while x > 0:
    y = float(input("Parking duration for car: "))
    if y <= 3:
        fee = 2
        print("Parking fee (RM): 2")
    elif 3 < y <= 4:
        fee = 2.50
        print("Parking fee (RM): 2.50")
    elif 4 < y <= 5:
        fee = 3
        print("Parking fee (RM): 3")
    elif 5 < y <= 6:
        fee = 3.50
        print("Parking fee (RM): 3.50")
    elif 6 < y <= 7:
        fee = 4
        print("Parking fee (RM): 4")
    elif 7 < y <= 8:
        fee = 4.50
        print("Parking fee (RM): 4.50")
    elif 8 < y <= 9:
        fee = 5
        print("Parking fee (RM): 5")
    elif 9 < y <= 10:
        fee = 5.50
        print("Parking fee (RM): 5.50")
    elif 10 < y <= 11:
        fee = 6
        print("Parking fee (RM): 6")
    elif 11 < y <= 12:
        fee = 6.50
        print("Parking fee (RM): 6.50")
    elif 12 < y <= 13:
        fee = 7
        print("Parking fee (RM): 7")
    elif 13 < y <= 14:
        fee = 7.50
        print("Parking fee (RM): 7.50")
    elif 14 < y <= 15:
        fee = 8
        print("Parking fee (RM): 8")
    elif 15 < y <= 16:
        fee = 8.50
        print("Parking fee (RM): 8.50")
    elif 16 < y <= 17:
        fee = 9
        print("Parking fee (RM): 9")
    elif 17 < y <= 18:
        fee = 9.50
        print("Parking fee (RM): 9.50")
    elif 18 < y <= 24:
        fee = 10
        print("Parking fee (RM): 10")
    x = x - 1
    sum = sum + fee

avg = sum / z
print("Total Fee (RM):", sum)
print("Average (RM):")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.