Feb-08-2021, 03:45 PM
(This post was last modified: Feb-08-2021, 04:45 PM by ButtNut_Squash.)
Can someone help me with this? I'm not sure what I have done wrong.
Exercise 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.
smallest = None
largest = None
while True:
num = input("Enter a number: ")
if num == "done" :
break
try:
fnum = float(num)
except:
print("Invalid input")
continue
if smallest is None:
smallest = num
elif num < smallest:
smallest = num
if largest is None:
largest = num
elif num > largest:
largest = num
print("Maximum is", largest)
print("Minimum is", smallest)
This is what I got:
Invalid input
Maximum is 7 ← Mismatch
Minimum is 10
This is what I was supposed to get:
Invalid input
Maximum is 10
Minimum is 2
Exercise 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.
smallest = None
largest = None
while True:
num = input("Enter a number: ")
if num == "done" :
break
try:
fnum = float(num)
except:
print("Invalid input")
continue
if smallest is None:
smallest = num
elif num < smallest:
smallest = num
if largest is None:
largest = num
elif num > largest:
largest = num
print("Maximum is", largest)
print("Minimum is", smallest)
This is what I got:
Invalid input
Maximum is 7 ← Mismatch
Minimum is 10
This is what I was supposed to get:
Invalid input
Maximum is 10
Minimum is 2
buran write Feb-08-2021, 04:08 PM:
Please, don't post screenshots. Copy/paste as code and traceback as text and use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Please, don't post screenshots. Copy/paste as code and traceback as text and use proper tags when post code, traceback, output, etc.
See BBcode help for more info.