# Club Results Tracker: names and 5 km cross-country ski times (minutes).
def show_menu():
"""Print the menu choices."""
print()
print("1) Add a result 2) Show all 3) Statistics")
print("4) Find a skier 5) Sort by time 6) Quit")
def read_time():
"""Ask until the user enters a positive number; return it as a float."""
while True:
text = input("Time in minutes: ")
if text.replace(".", "", 1).isdigit() and float(text) > 0:
return float(text)
print("Please enter a positive number, e.g. 21.5")
def find(names, target):
"""Return the index of target in names (ignoring case), or -1."""
for i in range(len(names)):
if names[i].lower() == target.lower():
return i
return -1
def average(values):
"""Return the mean of a non-empty list."""
total = 0
for v in values:
total += v
return total / len(values)
def fastest(values):
"""Return the index of the smallest value in a non-empty list."""
best = 0
for i in range(1, len(values)):
if values[i] < values[best]:
best = i
return best
def sort_by_time(names, times):
"""Selection sort on times, keeping names parallel (modifies both)."""
for start in range(len(times) - 1):
small = start
for i in range(start + 1, len(times)):
if times[i] < times[small]:
small = i
times[start], times[small] = times[small], times[start]
names[start], names[small] = names[small], names[start]
def show_all(names, times):
"""Print every skier and time, numbered from 1."""
for i in range(len(names)):
print(str(i + 1) + ".", names[i], times[i])
Part 1: the helper functions. Notice the header comment, the docstrings, and that only show_menu, read_time and show_all use input() or print().