[source code] Python Programming Tutorial - 28 - You are the only Exception

0 Bucky Roberts · September 9, 2014

# exceptions are not syntax errors, they are problems that occur while your programs running


# tuna = int(input("Whats your fav number?\n"))
# print(tuna)


while True:
   try:
       number = int(input("Whats your fav number?\n"))
       print(18/number)
       break
   except ValueError:
       print("Durrr, try again hoss.")
   except ZeroDivisionError:
       print("Don't pick zero")
   except:
       # not usually good because it can hide problems
       break
   finally:
       print('loop complete')

Post a Reply

Replies

Oldest  Newest  Rating
+1 Jay Tauron · September 9, 2014
you could use isinstance()
number = int(input("What's your favourite number?\n"))
#69
print( isinstance(number, int) )
#True
0 Nathanael Potter · September 9, 2014
Thanks Bucky, how would you make sure the user actually enters a number? (Instead of just leaving it blank). I tried using 'len' but that doesn't work on integers..
  • 1

Python

107,217 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator