Question

Write a program in Python using loop that asks for the names of three runners and...

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.

Homework Answers

Answer #1

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:

       

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program using Matlab that asks the user for their last three grades and final...
Write a program using Matlab that asks the user for their last three grades and final grade and gives them their overall final grade. The three Midterms are worth 70% and Final is worth 30%. The program will display their Final grade with a grade letter corresponding to it. Run your program and verify that it operates correctly (hint: you might need to use “If, else statements”)
IN PYTHON : Write a program that asks the user for a number. Write the number...
IN PYTHON : Write a program that asks the user for a number. Write the number to a file called total.txt. Then ask the user for a second number. Write the new number on line 2 with the total for both numbers next to it separated by a comma. Then ask the user for a third number and do the same. Keep asking for numbers until the person puts in a zero to terminate the program. Close the file. Be...
Today we are practicing our ABC's. Write a program that will accept three separate names as...
Today we are practicing our ABC's. Write a program that will accept three separate names as inputs (ex: "billy"). Then display the names in alphabetical order. The names will always use all lowercase letters. For example: given the input "lauren" "rodger" "bob" The output should be "bob" "lauren" "rodger" Note: Think carefully about the logic needed to solve this problem and remember there are only three names that need to be sorted. P.S. The computer language we are using is...
Python code only! (Do not include breaks or continue functions) Write a program that asks the...
Python code only! (Do not include breaks or continue functions) Write a program that asks the user to enter the amount that they budgeted for the month. A loop should then prompt the user to enter their expenses, one at a time and to enter 0 to quit. When the loop finishes, the program should display the the amount of budget left. (a negative number indicates the user is over budget and a positive number indicates the user is under...
python Write a program to find the largest value. Ask the user to enter three numbers....
python Write a program to find the largest value. Ask the user to enter three numbers. Compare them and report the largest one. [Hint: The user is free to enter any three numbers. Some or all of them may be the same. Take this into consideration when you compare the numbers.] The following are some examples. Enter first number: 7 Enter second number: 14 Enter third number: 10.5 The largest number is: 14.0 Enter first number: 17 Enter second number:...
Python programming Write a program that prompts the user to input the three coefficients a, b,...
Python programming Write a program that prompts the user to input the three coefficients a, b, and c of a quadratic equationax2+bx+c= 0.The program should display the solutions of this equation, in the following manner: 1. If the equation has one solution, display ONE SOLUTION:, followed by the solution, displayed with4 digits printed out after the decimal place. 2. If the equation has two real solutions, display TWO REAL SOLUTIONS:, followed by the two solutions, each displayed with4 digits printed...
Python Programming Build a python programming that asks the user for a three-letter substring. The program...
Python Programming Build a python programming that asks the user for a three-letter substring. The program should then proceed to read the file strings.txt. Each line in strings.txt will contain a string. Your program should then test to see how many non-overlapping occurrences of the three-letter substring occur in that string and test whether the string is a palindrome. The program should then proceed to create an output file string_stats.txt, which contains the original data on one line and the...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
C++ Fahrenheit to Celsius Tables Write a program that first asks the user which Temperature scale...
C++ Fahrenheit to Celsius Tables Write a program that first asks the user which Temperature scale conversion he/she would like to perform: 1. Convert F to C 2. Convert C to F 3. Quit What is your choice? Then it asks the user for input for three real number variables: start_temp, end_temp, temp_incr. It will then produce a two column Fahrenheit to Celsius table or a two column Celsius to Fahrenheit table, depending on the choice. For choice 1, the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT