Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 21st, 2013, 10:31 PM
kendojosh kendojosh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 kendojosh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 11 sec
Reputation Power: 0
Error Handling

I have been doing a beginner's tutorial for Python (using v2.7.3 on lubuntu 12.10)
and I have encountered an error handling issue I don't understand.

I've written the following code while playing around with the error handling to see how it works:

loop = 1
while loop == 1:
try:
a = input("Subtract this: ")
b = input("from this: ")
except NameError:
print"\nChoose a number not a letter, you muppet"
continue
except SyntaxError:
print"\nWhat was that gibberish? Enter a number!"
continue
except ZeroDivisionError:
print"\nDivide by zero?"
print"\nAre you trying to destroy the universe??"
continue
except TypeError:
print"\nWat? I don't even..."
continue
print b,"-",a,"=",b-a
try:
loop = input("Enter '1' to continue, anything else to quit: ")
except (NameError, SyntaxError, ZeroDivisionError, TypeError):
loop = 0


(by the way, in the real code all the indents are there and correct as far as I can tell - the forum seems to dislike me using indents in the post)

Anyhow, I added 'except TypeError' because that was what I was getting if I entered 'quit' (as opposed to 'quit()') as any of the variables.
As you can see I've attempted to handle TypeError the same way I handled the other Errors (which all function properly).

The problem is that the program still crashes and throws up a 'TypeError' even though I have added code that should handle that... why?

Can anyone answer this?

Reply With Quote
  #2  
Old January 21st, 2013, 11:31 PM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 313 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 23 h 10 m 2 sec
Reputation Power: 7
How input works depends on which version of Python you are using. I am assuming Python 3.x which returns string. Generally it is something like
Code:
loop = 1
while loop == 1:
    try:
        a = input("Subtract this: ")  ## assume this returns a string
        a_int = int(a)              ## assume integers and not floats
        b = input("from this: ")
        b_int = int(b)
        print b,"-",a,"=",b-a
    except:
        print "Enter numbers only" 


In the following, test for "1" and exit otherwise. There is no need for a try/except especially since you say "anything else to quit" so the user should be able to enter anything.
Code:
    try:
        loop = input("Enter '1' to continue, anything else to quit: ")
    except (NameError, SyntaxError, ZeroDivisionError, TypeError):
        loop = 0 

Original post quoted with indents for reference. Click the hash, #, at the top and post code in between to preserve indents.
Code:
loop = 1
while loop == 1:
    try:
        a = input("Subtract this: ")
        b = input("from this: ")
    except NameError:
        print"\nChoose a number not a letter, you muppet"
        continue
    except SyntaxError:
        print"\nWhat was that gibberish? Enter a number!"
        continue
    except ZeroDivisionError:
        print"\nDivide by zero?"
        print"\nAre you trying to destroy the universe??"
        continue
    except TypeError:
        print"\nWat? I don't even..."
        continue
    print b,"-",a,"=",b-a
    try:
        loop = input("Enter '1' to continue, anything else to quit: ")
    except (NameError, SyntaxError, ZeroDivisionError, TypeError):
        loop = 0 

Last edited by dwblas : January 21st, 2013 at 11:42 PM.

Reply With Quote
  #3  
Old January 22nd, 2013, 05:57 PM
kendojosh kendojosh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 kendojosh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 11 sec
Reputation Power: 0
I am using Python 2.7 which unless I am mistaken returns numbers for 'input' and strings for 'raw_input'

"There is no need for a try/except especially since you say "anything else to quit" so the user should be able to enter anything"
In this line, entering '1' keeps the loop in play, entering another number breaks the loop. Entering a letter or a symbol causes python to **** a brick, that's why there is a 'Try'/'Except' to direct the program to break the loop (by making loop = 0).

What I am still unclear on is why I can get the program to respond to 'NameError', 'SyntaxError', and 'ZeroDivisionError' in the manner that I choose, but I can't get it to do the same for 'TypeError'...

(thanks for the tip on displaying code, btw)

Reply With Quote
  #4  
Old January 22nd, 2013, 06:25 PM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 313 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 23 h 10 m 2 sec
Reputation Power: 7
Use raw_input with try/except to catch non_numbers. Code as posted above but with raw_input instead, and again assumes integers and not floats. Note that you have nothing in the code that will trigger a Zero Division, Name Error, or Syntax Error.
Code:
loop = 1
while loop == 1:
    print
    try:
        a = raw_input("Subtract this: ")
        a_int = int(a)              ## non_number triggers except
        b = raw_input("from this: ")
        b_int = int(b)              ## non_number triggers except
        print "%s - %s = %d" % (b, a, b_int-a_int)


        choice = raw_input("Enter '1' to continue, anything else to quit: ")
        if choice != '1':  ## compares strings not numbers
            loop = 0
    except:
        print "Enter numbers only"  

Last edited by dwblas : January 22nd, 2013 at 06:37 PM.

Reply With Quote
  #5  
Old January 22nd, 2013, 10:00 PM
kendojosh kendojosh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 kendojosh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 11 sec
Reputation Power: 0
As suggested I used raw_input to filter out non-integers. I modified my code like so:

Code:
###ErrorHandling.py

#Error handling using the example of a simple subtraction program

#Program begins
print"Subtracting the Error v0.2 \n\n"

loop = 1
while loop == 1:
    try:
        a = raw_input("Subtract this: ")
        b = raw_input("from this: ")
        c = int(a)
        d = int(b)
    except ValueError:
        print"\nOh no! an error occurred!"\
                   "\nDon't input letters, words, or bad equations, ok!\n\n"
        continue
    
    print d,"-",c,"=",d-c,"\n\n"
    choice = raw_input("Enter '1' to continue, anything else to quit...\n\n")
    if choice != "1":
        loop = 0
        
#Program ends 
print"Thank you for subtracting some time to use this program!"


And now, as far as I can tell, I've met the tutorial's goal of making this small program uncrashable (if there's a way to crash it, please let me know).

Though I would still like to know why a TypeError was able to crash the original iteration of the program even though I had an 'except' to deal with TypeErrors.

Thanks for your help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Error Handling

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap