Skip to content

Instantly share code, notes, and snippets.

@MichelleDalalJian
Created October 7, 2017 12:44
Show Gist options
  • Save MichelleDalalJian/94618b42f88e35e6f5dc054f8ef6dc2d to your computer and use it in GitHub Desktop.
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…
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)
@BluHope
Copy link

BluHope commented Oct 8, 2023

And what should be written in the code checking section? I don't know what to write

@Muskan12344567
Copy link

I was entering those numbers 7,2,10,4 and done but its showing again enter the number

@dikubab
Copy link

dikubab commented Apr 2, 2024

`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.")`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment