Question

In: Computer Science

Write a function printTwoLargest() that inputs an arbitrary number of positive numbers from the user. The...

  1. Write a function printTwoLargest() that inputs an arbitrary number of positive numbers from the user. The input of numbers stops when the first negative or zero value is entered by the user. The function then prints the two largest values entered by the user. If no positive numbers are entered a message to that effect is printed instead of printing any numbers. If only one number is inputted, only the largest is printed out (see 2nd example below). Sample output:

>>> printTwoLargest()

Please enter a number: 12

Please enter a number: 99.9

Please enter a number: 4.5

Please enter a number: 77

Please enter a number: 0

The largest is 99.9

The second largest is 77

>>> printTwoLargest()

Please enter a number: 23.2

Please enter a number: -99

The largest is 23.2

>>> printTwoLargest()

Please enter a number: -9

No positive numbers were entered

>>>

  1. Test your code by downloading the file lab1TEST.py in the same working folder and add the following code to the bottom of your lab1.py module. Run and fix errors before you submit.

if __name__=='__main__':
    import doctest
    print( doctest.testfile( 'lab1TEST.py' ))

use puthon 3.7

Solutions

Expert Solution

If you need any corrections/clarifications kindly comment.

Please give a Thumps Up if you like the answer.

Program

def printTwoLargest(list1):
    largest = list1[0]
    largest2 = None
  
    for item in list1[1:]:    
        if item > largest:
            largest2 = largest
            largest = item
        elif largest2 == None or largest2 < item:
            largest2 = item
      
            
    print("The largest is:", largest)
    print("The second largest is:", largest2)



# Driver Code
numList = []
num = 0

while True:
        num = float(input('Please enter a number: '))
        if num >0:
            numList.append(num)
        else:
            break
if len(numList)==0:
    print("No positive numbers were entered")
elif len(numList)==1:
    print("The largest is",numList[0])
else:
    printTwoLargest(numList)

Output

>>>
=============== RESTART: /home/user/PythonPrograms/largest.py ===============
Please enter a number: 12
Please enter a number: 99.9
Please enter a number: 4.5
Please enter a number: 77
Please enter a number: 0
The largest is: 99.9
The second largest is: 77.0
>>>
=============== RESTART: /home/user/PythonPrograms/largest.py ===============
Please enter a number: 23.2
Please enter a number: -99
The largest is 23.2
>>>
=============== RESTART: /home/user/PythonPrograms/largest.py ===============
Please enter a number: -9
No positive numbers were entered
>>>

Screenshot

def printTwoLargest(listi): largest = listi[0] largest2 = None for item in list1[1:]: if item > largest: largest2 = largest largest = item elif largest2 == None or largest2 < item: largest2 = item print("The largest is:", largest) print("The second largest is:", largest2) # Driver Code numList = [] num = 0 while True: num = float(input('Please enter a number: ')) if num >: numList.append(num) else: break if len(numList)==0: print("No positive numbers were entered") elif len(numList)==1: print("The largest is", numList[0]) else: printTwoLargest(numList)


Related Solutions

Write a function posnum that prompts the user to enter a positive number and loops to...
Write a function posnum that prompts the user to enter a positive number and loops to error-check. It returns the square root of the positive number entered by the user. It calls a subfunction in the loop to print an error message. The subfunction has a persistent variable to count the number of times an error has occurred. Here is an example of calling the function: >> squarerootvalue = posnum Enter a positive number: -5 Error #1: Follow instructions! Does...
Write a function that inputs a list of numbers and a percentile value from 0 to...
Write a function that inputs a list of numbers and a percentile value from 0 to 1 and have the function return the element that is at that percentile (or greater than). You can use this website for the steps https://www.indeed.com/career-advice/career-development/how-to-calculate-percentile check out the step for “Follow these steps to calculate the kth percentile” def percentileReturn(list1,percentile):#function which returns the list of elements which are greater than or equal to the percentile list2=[i for i in list1 if i>=percentile] #creating list2...
1. Write a function named “Number” that reads in a list of numbers until the user...
1. Write a function named “Number” that reads in a list of numbers until the user enters 0. It will return true if the user has entered more even numbers than odd numbers; otherwise it returns false. 2. Write a code segment to sort an array of students in ascending order of their IDs. Assume the array has been filled with names and assume the following declaration: struct Student { string name; int ID; } Student roster[30]; // Code to...
C++ program that reads a positive integer number from a user and displays all binary numbers...
C++ program that reads a positive integer number from a user and displays all binary numbers from 0 to the number. For the program, you can assume that the input number is a number between 0 and 100. Sample Run 1: Assume that the user typed the following number. 5 This is the correct output of your program. 000 001 010 011 100 101 Sample Run 2: Assume that the user typed the following number. 0 This is the correct...
write a VBA code to convert an arbitrary positive binary fractional number (0< number<1) to equivalent...
write a VBA code to convert an arbitrary positive binary fractional number (0< number<1) to equivalent decimal number. the code should acquire the binary fraction number in the format"0.xxxxxx"from input box, then return the equivalent decimal number in a message box. in the code, you may need to use VBA function "mid(_,_,_)" to pick up a specific symbols or characters from a string. you can use below conversion as benchmark to verify and debug your code: (0.1011)2 = (0.6875)10   
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0...
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0 and prints in a column all of its divisors including 1 and the number itself. Don't forget that printing a number and printing a character requires two different system calls. Here is an example. If the number in a0 is 6 the following output appears: 1 2 3 6
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the form (555) 555-5555. The program should use function strtok to extract the area code as a token, the first three digits of the telephone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The program should convert the area-code string to int and convert...
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
Describe the algorithm to generate random numbers from an arbitrary discrete distribution with finite number of...
Describe the algorithm to generate random numbers from an arbitrary discrete distribution with finite number of outcomes.
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT