Skip to content

Instantly share code, notes, and snippets.

@zcakzwa
Created July 5, 2016 05:01
Show Gist options
  • Save zcakzwa/aebffa8eecd13022ba125b03a721b247 to your computer and use it in GitHub Desktop.
Save zcakzwa/aebffa8eecd13022ba125b03a721b247 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 the numbers from the book …
largest = None
smallest = None
while True:
num = raw_input("Enter a number: ")
if num == "done" : break
try:
num = int(num)
except:
print "Invalid input"
continue
if largest is None:
largest = num
elif num > largest:
largest = num
if smallest is None:
smallest = num
elif num < smallest:
smallest = num
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