In: Computer Science
>>> 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
>>>
if __name__=='__main__':
import doctest
print( doctest.testfile( 'lab1TEST.py'
))
use puthon 3.7
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)