21,718 views
24 votes
24 votes
Design the program in the following way:

a. Prompt the user for the name of a boat and then for that boat's time (in seconds).
b. Continue to do this until the user presses enter without entering a name or enters a sentinel value.
c. At this point output the name of the race winner along with the time, the average time, the name the slowest boat along with its time.
d. Use a map structure to store the boat names and their associated times. You may use lists within the program as you see fit.

User FatBruno
by
2.5k points

1 Answer

25 votes
25 votes

Answer:

d = {}

while True:

name = input("Enter the name of the boat (Press Enter or q to stop): ")

if name == "" or name == "q":

break

time = int(input("Enter the time (in seconds): "))

d[name] = time

winner_name = min(d, key=d.get)

winner_time = min(d.values())

average_time = sum(d.values()) / len(d.values())

slowest_boat = max(d, key=d.get)

slowest_time = max(d.values())

print(f"Winner name: {winner_name} and its time: {winner_time}")

print(f"Average time: {average_time}")

print(f"Slowest boat: {slowest_boat} and its time: {slowest_time}")

Step-by-step explanation:

*The code is in Python.

Create an empty map(dictionary) to hold the values

Create a while loop. Inside the loop:

Ask the user to enter the name of the boat. Check if the user presses Enter or q, using if and break

Ask the user to enter the time

Insert these values as key-value pair to the map

When the loop is done:

Find the winner_name, the min() function finds the minimum value in a list. In this case, the key=d.get creates a list of the values in the map. The min(d, key=d.get) returns the key of the minimum value

Find the winner_time, min(d.values()) returns the minimum time in our map

Find the average time, the sum() function, returns the sum of the values in a list. In this case, sums all the values in the map. The len() function finds the count of the values. If we divide the sum of times entered by the count of the times entered, we get the average time

Find the slowest_time, the max() function returns the max of the value in a list. In this case, the maximum value is the slowest time

Print the results

User Hrezs
by
2.8k points