In: Computer Science
Using LIST and FUNCTION
Write a program in Python 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.
Python code:
#function to print first,second and third
def first(nameList,timeList):
#checking if the first name came first
if(timeList[0]<timeList[1] and
timeList[0]<timeList[2]):
#printing First person
print("First:",nameList[0])
#checking if second name is second
if(timeList[1]<timeList[2]):
#printing Second person
print("Second:",nameList[1])
#printing Third person
print("Third:",nameList[2])
else:
#printing Second person
print("Second:",nameList[2])
#printing Third person
print("Third:",nameList[1])
#checking if the second name came first
elif(timeList[1]<timeList[0] and
timeList[1]<timeList[2]):
#printing First
print("First:",nameList[1])
#checking if first name is second
if(timeList[0]<timeList[2]):
#printing Second person
print("Second:",nameList[0])
#printing Third person
print("Third:",nameList[2])
else:
#printing Second person
print("Second:",nameList[2])
print("Third:",nameList[0])
#checking if the thrid name came first
elif(timeList[2]<timeList[0] and
timeList[2]<timeList[1]):
#printing First
print("First:",nameList[2])
#checking if first name is second
if(timeList[0]<timeList[1]):
#printing Second person
print("Second:",nameList[0])
#printing Third person
print("Third:",nameList[1])
else:
#printing Second person
print("Second:",nameList[1])
#printing Third person
print("Third:",nameList[0])
#initializing an empty nameList
nameList=[]
#initializing an empty timeList
timeList=[]
#loop to accept names and times
for i in range(3):
#accepting name into list
nameList.append(input("Enter the name: "))
#accepting time into list
timeList.append(int(input("Enter time: ")))
#calling first function
first(nameList,timeList)
Screenshot:
Input and Output: