Coursera
Programming for Everybody (Getting Started with Python)
Week 5
Assignment 5.2
Question: 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters ‘done’. Once ‘done’ is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.
Input:
largest = None
smallest = None
while True:
num = raw_input("Enter a number: ")
if num == "done": break
try:
num = int(num)
if largest is None or largest < num: largest = num
if smallest is None or smallest > num: smallest = num
except:
print ("Invalid input")
continue
print ("Maximum is",largest)
print ("Minimum is",smallest)
Output:
Invalid input
Maximum is 10
Minimum is 2
The above questions are from “Programming for Everybody (Getting Started with Python)” You can discover all the refreshed questions and answers related to this on the “Programming for Everybody (Getting Started with Python) By Coursera” page. If you find the updated questions or answers, do comment on this page and let us know. We will update the answers as soon as possible.
Do Not Only Use These Quizzes For Getting Certificates.You Can Take Help From These Quizzes Answer. All Quizzes & Contents Are Free Of Charge. ✅If You Want Any Quiz Answers Then Please Contact Us