I solved this practice problem in preparation for school exams.
The owner of a taxi company wants a system that calculates how much money his taxis take in one day.
Write and test a program for the owner.
Your program must include appropriate prompts for the entry of data.
Error messages and other output need to be set out clearly and understandably.
All variables, constants and other identifiers must have meaningful names.
You will need to complete these three tasks. Each task must be fully tested.
TASK 1 – calculate the money owed for one trip
The cost of a trip in a taxi is calculated based on the numbers of KMs traveled and the type of taxi that you are traveling in. The taxi company has three different types of taxi available:
- a saloon car, that seats up to 4,
- a people carrier, that seats up 8,
- a mini-van, that seats up to 12.
For a trip in a saloon car the base amount is RM2.50 and then a charge of RM1.00 per KM. For a trip in a people carrier the base amount is RM4.00 and a charge of RM1.25 per KM. For a trip in a mini- van the base amount is RM5.00 and a charge of RM1.50 per KM. The minimum trip length is 3KM and the maximum trip length is 50KM. Once the trip is complete a 6% service tax is added.
TASK 2 – record what happens in a day
The owner of the taxi company wants to keep a record of the journeys done by each one of his taxis in a day. For each one of his three taxis record the length of each trip and the number of people carried. Your program should store a maximum of 24 trips or 350km worth of trips, whichever comes first. Your program should be able to output a list of the jobs done by each one of the three taxis.
TASK 3 – calculate the money taken for all the taxis at the end of the day.
At the end of the day use the data stored for each taxi to calculate the total amount of money taken and the total number of people carried by each one of the three taxis. Using the average price of RM2.79 per litre use the information in the table below to calculate the fuel cost for each taxi:
- the saloon car uses 7.4L/100km
- the people carrier uses 8.6L/100km
- and the mini-van uses 9.2L/100km
Provide a report for the owner to show this information.
Here is my rather messy solution:
saloon = []
people_car = []
mini = []
count = 0
total_seats = 0
total_cost = 0
mini_distance = 0
sln_distance = 0
ppl_distance = 0
def check(distance):
try:
distance = float(distance)
except ValueError:
print("Enter a number")
return False
while True:
print("[1] Add to List (" + repr(count) + " entered)")
print("[2] View List")
print("[3] View Information and Costs")
print("[4] Exit")
menu = input("Enter command: ")
if menu == "1":
clarify = input("Add to list? (yes/no) ")
if clarify == "yes" and count < 25:
while True:
if count > 24:
print("You have reached the maximum amount of list inputs!")
break
seats = input("How many riders? (type exit to exit) ")
try:
seats = int(seats)
except ValueError:
print("Please enter a number \n=======================================")
pass
break
if int(seats) > 0 and int(seats) < 5:
car_type = input("You will be taking the saloon car? (yes/no) ")
if car_type == "yes":
distance = input("How far was the trip? ")
if check(distance) == False:
break
if distance > 0 and distance < 51 and sln_distance < 351:
count += 1
len_sum = float(2.50) + distance
total_sum = len_sum * 1.06
print("Price is: RM" + repr(round(total_sum, 2)) + "\n=======================================")
saloon.append("Riders: " + repr(seats) + "Revenue: RM" + repr(round(total_sum, 2)) + " | ")
total_seats += seats
total_cost += round(total_sum, 2)
sln_distance += distance
elif sln_distance > 350:
print("This car has reached it's limit")
elif distance < 1 or distance > 50:
print("Please enter a valid distance between 1 and 50")
else:
print("Have you tried digits?")
elif car_type == "no":
print("Command cancelled")
else:
print("Please redo")
if int(seats) > 4 and int(seats) < 9:
car_type = input("You will be taking the people carrier? (yes/no)")
if car_type == "yes":
distance = input("How far was the trip? ")
if check(distance) == False:
break
if distance > 0 and distance < 51:
count += 1
dis_cos = distance * 1.25
len_sum = float(4) + dis_cos
total_sum = len_sum * 1.06
print("Price is: RM" + repr(round(total_sum, 2)) + "\n=======================================")
people_car.append("Riders: " + repr(seats) + "Revenue: RM" + repr(round(total_sum, 2)) + " | ")
total_seats += seats
total_cost += round(total_sum, 2)
ppl_distance += distance
elif ppl_distance > 350:
print("This car has reached it's limit")
elif distance < 1 or distance > 50:
print("Please enter a valid distance between 1 and 50")
else:
print("Have you tried digits?")
elif car_type == "no":
print("Please redo")
else:
print("Please redo")
if int(seats) > 8 and int(seats) < 13:
car_type = input("You will be taking the mini van? (yes/no)")
if car_type == "yes":
distance = input("How far was the trip? ")
if check(distance) == False:
break
if distance > 0 and distance < 51:
count += 1
dis_cos = distance * 1.5
len_sum = float(5) + dis_cos
total_sum = len_sum * 1.06
print("Price is: RM" + repr(round(total_sum, 2)) + "\n=======================================")
mini.append("Riders: " + repr(seats) + "Revenue: RM" + repr(round(total_sum, 2)) + " | ")
total_seats += seats
total_cost += round(total_sum, 2)
mini_distance += distance
elif mini_distance > 350:
print("This car has reached it's limit")
elif distance < 1 or distance > 50:
print("Please enter a valid distance between 1 and 50")
else:
print("Have you tried digits?")
elif car_type == "no":
print("Please redo")
else:
print("Please enter a valid answer")
elif seats == "exit":
break
elif seats < 1 or seats > 12:
print("Enter within the given range")
elif (not int(seats) > 0) or (not int(seats) < 13) or (not seats == "exit"):
print("========================================")
if clarify == "no":
break
else:
continue
else:
print("Please redo \n=======================================")
elif count == int(24):
count += 1
print("You have reached the maximum amount of list inputs!")
elif menu == "2":
print("Saloon: " + ' '.join(saloon) + "\nPeople carrier: " + ' '.join(people_car) + "\nMini van: " + ' '.join(mini))
print("========================================")
elif menu == "3":
print("Riders: " + repr(total_seats) + "\nCost: RM" + repr(total_cost))
def calc(a):
avg_cost = float(a) / 100
return avg_cost * 7.4
final_sln = float(calc(sln_distance))
final_ppl = float(calc(ppl_distance))
final_mini = float(calc(mini_distance))
print("Saloon fuel cost: RM" + repr(round(final_sln, 2)))
print("People carrier fuel cost: RM" + repr(round(final_ppl, 2)))
print("Mini van fuel cost: RM" + repr(round(final_mini, 2)))
print("========================================")
elif menu == "4":
prompt = input("Exit program? (yes/no)")
if prompt == "yes":
exit()
else:
print("Command canceled")
continue