Skip to content

Instantly share code, notes, and snippets.

@Mohamed2del
Created June 9, 2018 16:51
Show Gist options
  • Save Mohamed2del/4e513c9c039849d05867852522932c73 to your computer and use it in GitHub Desktop.
Save Mohamed2del/4e513c9c039849d05867852522932c73 to your computer and use it in GitHub Desktop.
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…
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" : break
try : n = int(num)
except :
print('Invalid input')
continue
if largest is None:
largest = n
elif n > largest:
largest = n
if smallest is None:
smallest = n
elif n < smallest:
smallest = n
print("Maximum is", largest)
print("Minimum is", smallest)
@Pallavi-Tatiparti
Copy link

Pallavi-Tatiparti commented Jul 27, 2020

Thank you so much for posting the code. I had written the same code but it was showing error on the line 5 so many times ...Anyways with your code my problem got solved .. Thanks again!

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