handling python errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    handling python errors

    hey everyone,

    i'm working with a list that would be something like
    xx = ['0.53', 'n/a', '0.36', '0.45', '0.60']

    and i want to do some calcuations with each item, lets say subtract one item from the item prior...not bad say something like...

    Code:
    results = []
    while second < len(xx):
        first = 0
        second = 1
        results.append(xx[second] - xx[first])
        first += 1
        secodn += 1
    that wouldnt be too bad... if it weren't for the n/a. And its important to keep that n/a in there.

    what i'd like is, if the code is soppose to do soemthing like n/a - 0.53 for it to return "BadMath".. .

    But when i use the code above i get the following error...
    Code:
    Traceback (most recent call last):
      File "<interactive input>", line 4, in <module>
    ValueError: invalid literal for float(): n/a
    Any ideas how i could get around that? Thanks

    PC
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Patrick C
    hey everyone,

    i'm working with a list that would be something like
    xx = ['0.53', 'n/a', '0.36', '0.45', '0.60']

    and i want to do some calcuations with each item, lets say subtract one item from the item prior...not bad say something like...

    Code:
    results = []
    while second < len(xx):
        first = 0
        second = 1
        results.append(xx[second] - xx[first])
        first += 1
        secodn += 1
    that wouldnt be too bad... if it weren't for the n/a. And its important to keep that n/a in there.

    what i'd like is, if the code is soppose to do soemthing like n/a - 0.53 for it to return "BadMath".. .

    But when i use the code above i get the following error...
    Code:
    Traceback (most recent call last):
      File "<interactive input>", line 4, in <module>
    ValueError: invalid literal for float(): n/a
    Any ideas how i could get around that? Thanks

    PC
    You can use a try and except block:
    [code=python]
    try:
    # code that might raise an exception here
    except ValueError:
    # code the runs if the specified exception is raised

    # program resumes
    [/code]
    Also, you while loop seems wrong.

    Comment

    • Patrick C
      New Member
      • Apr 2007
      • 54

      #3
      Thanks for your suggestion on the try/except method. I tried to implement it into my fixed (hopefully) while loop. But I'm having problems.

      I put 2 try/except loops in it because at different stages I'd get different errors.
      What I was hoping to end up with was
      sub = -.17, error(of some type), error(of some type), .15

      I say error(of some type) because i'm not sure which error it would yield.

      here's what I have
      Code:
      >>> stuff2 = ['0.53', '0.36', 'n/a', '0.45', '0.60']
      >>> bb = 1
      >>> aa = 0
      >>> sub = []
      >>> sub
      []
      >>> while bb < len(stuff2):
      ... 	try:
      ... 		sub.append(stuff2[bb] - stuff2[aa])
      ... 		bb += 1
      ... 		aa += 1
      ... 		try:
      ... 			sub.append(stuff2[bb] - stuff2[aa])
      ... 			bb += 1
      ... 			aa += 1
      ... 		except ValueError:
      ... 			sub.append("error1")		
      ... 			bb += 1
      ... 			aa += 1
      ... 	except TypeError:		
      ... 		sub.append("error2")
      ... 		bb += 1
      ... 		aa += 1
      ... 
      >>> sub
      ['error2', 'error2', 'error2', 'error2']

      Comment

      • Patrick C
        New Member
        • Apr 2007
        • 54

        #4
        Ignore that, i forgot that stuff2 is still a string, if i still am having a problem, i'll put another post up.

        thanks

        Comment

        Working...