PYTHON Write a program that calculates and displays the parking charges for each customer. Function main calls function calculate_charges. Function calculate_charges reads file hours.txt.

Respuesta :

Answer:

  1. HOUR_RATE = 2.5
  2. count = 1
  3. with open("hours.txt",'r') as file:
  4.    rows = file.readlines()
  5.    for h in rows:
  6.        charge = float(h) * HOUR_RATE
  7.        print("Customer " + str(count) + " parking charge: $" + str(charge))
  8.        count += 1

Explanation:

The solution code is written in Python 3

Firstly we set the hourly parking rate as $2.5 (Line 1) and create a counter variable with starting value 1 (Line 2). This is track the customer number from the text file.

Next open the file stream and use readlines method to read each row of the parking hour record (Line 3 -6). Apply the formula to calculate the parking charge for each customer and display the result. (Line 8) and increase the count by one before proceed to the next iteration.