Write a program in Python using loop that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place. Think about how many test cases are needed to verify that your problem works correctly.
Python code:
#initializing a list name to store the name
name=[]
#initializing a list time to store the time of each runner
time=[]
#loop to accept the time and name
for i in range(3):
#asking name of runner
name.append(input("Enter name of runner
"+str(i+1)+": "))
#asking for time took
time.append(int(input("Enter the time took to
fininsh the race(in seconds): ")))
#checking if the 1st runner took the shortest time
if(time[0]<time[1] and time[0]<time[2]):
#printing first is 1st runner
print("first: "+name[0])
#checking if the 2nd runner took the second
shortest time
if(time[1]<time[2]):
#printing second is 2nd
runner
print("second:
"+name[1])
#printing third is 3rd
runner
print("third:
"+name[2])
else:
#printing second is 3rd
runner
print("second:
"+name[2])
#printing third is 2nd
runner
print("third:
"+name[1])
#checking if the 2nd runner took the shortest time
elif(time[1]<time[0] and time[1]<time[2]):
#printing first is 2nd runner
print("first: "+name[1])
#checking if the 1st runner took the second
shortest time
if(time[0]<time[2]):
#printing second is 1st
runner
print("second:
"+name[0])
#printing third is 3rd
runner
print("third:
"+name[2])
else:
#printing second is 3rd
runner
print("second:
"+name[2])
#printing third is 1st
runner
print("third:
"+name[0])
else:
#printing first is 3rd runner
print("first: "+name[2])
#checking if the 1st runner took the second
shortest time
if(time[0]<time[1]):
#printing second is 1st
runner
print("second:
"+name[0])
#printing third is 2nd
runner
print("third:
"+name[1])
else:
#printing second is 2nd
runner
print("second:
"+name[1])
#printing third is 1st
runner
print("third:
"+name[0])
Screenshot:
Input and Output:
Get Answers For Free
Most questions answered within 1 hours.