Write a program (in python) 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.
Answers
Answer:
This is the assignment:
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.
This is my code:
Python Code: (Double-click to select all)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
largest = None
smallest = None
while True:
try:
num = raw_input("Enter a number: ")
if num == "done":
break
print (num)
num = int(num)
if largest is None or largest < num:
largest = num
elif smallest is None or smallest > num:
smallest = num
except ValueError:
print("Please, enter only numbers.")
print ("Maximum", largest)
print ("Minimum", smallest)
This is the Desired output:
Invalid input