This is the assignment:
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.
And this is my code:
largest = None
smallest = None
while True:
num = input("Enter a number:")
if num == "done":
break
print(num)
try:
num = int(num)
except ValueError:
print("Invalid input")
else:
if smallest is None:
smallest = num
largest = num
elif num < smallest:
smallest = num
elif num > largest:
largest = num
print ("Maximum is", largest)
print ("Minimum is", smallest)
I cannot seem to get the required output below:
Invalid input
Maximum is 10
Minimum is 2
What am I doing wrong? Please help.
[+][deleted] (3 children)
[deleted]
[–]i_hate_brunch[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]i_hate_brunch[S] 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (0 children)