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.
Answers
Program
Explanation:
"""
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.
"""
largest = None
smallest = None
while True:
try:
num = raw_input("Enter a number: ")
if num == 'done':
break;
n = int(num)
largest = num if largest < num or largest == None else largest
smallest = num if smallest > num or smallest == None else smallest
except:
print "Invalid input"
print "Maximum number is ", largest
print "Minimum number is ", smallest
Answer:
largest = None
smallest = None
while True:
try:
num = input("Enter a number: ")
if num == 'done':
break
n = int(num)
if largest is None or n > largest:
largest = n
elif smallest is None or n < smallest:
smallest = n
except:
print("Invalid input")
continue
print("Maximum is", largest)
print("Minimum is", smallest)
Explanation:
working code for coursera hw