Created
October 7, 2017 12:44
-
-
Save MichelleDalalJian/94618b42f88e35e6f5dc054f8ef6dc2d to your computer and use it in GitHub Desktop.
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 m…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
largest = None | |
smallest = None | |
the_list = [] | |
while True: | |
num = input() | |
if num == "done":break | |
if len(num) < 1: break | |
try: | |
number = int(num) | |
the_list.append(num) | |
except: | |
print("Invalid input") | |
largest = -1 | |
for the_num in [7,2,10,4]: | |
if the_num > largest: | |
largest = the_num | |
smallest = None | |
for value in [7,2,10,4]: | |
if smallest is None: | |
smallest = value | |
elif value < smallest: | |
smallest = value | |
print("Maximum is", largest) | |
print("Minimum is", smallest) |
I was entering those numbers 7,2,10,4 and done but its showing again enter the
What should we enter in the box
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
n = int(num)
if largest is None or n > largest:
largest = n
if smallest is None or n < smallest:
smallest = n
except ValueError:
print("Invalid input")
continue
print("Maximum is", largest)
print("Minimum is", smallest)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`largest = None
smallest = None
the_list = []
while True:
num = input("Enter a number: ")
if num == "done":
break
if len(num) < 1:
break
try:
number = int(num)
the_list.append(number)
except:
print("Invalid input. Please enter an integer number or 'done' to exit.")
if len(the_list) > 0:
largest = max(the_list)
smallest = min(the_list)
print("Maximum is", largest)
print("Minimum is", smallest)
else:
print("No valid numbers entered.")`