try/except within a while loop problem

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

    try/except within a while loop problem

    Hey,

    If i use a try/except clause to get around something like a TypeError, will that immediately bust me out of a while loop?

    If so, is there any way i can tell it to stay in the while loop until its completed.

    here's what I'm doing:

    Code:
    >>> stuff2
    ['0.53', '0.36', 'n/a', '0.45', '0.60']
    >>> aa = 0
    >>> bb = 1
    >>> sub = []
    >>> while bb < len(stuff2):
    ... 	try:
    ... 		sub.append(float(stuff2[bb]) - float(stuff2[aa]))
    ... 		bb += 1
    ... 		aa += 1
    ... 		try:
    ... 			sub.append(float(stuff2[bb]) - float(stuff2[aa]))
    ... 			bb += 1
    ... 			aa += 1
    ... 		except ValueError:
    ... 			sub.append("error1")		
    ... 			bb += 1
    ... 			aa += 1
    ... 	except TypeError:		
    ... 		sub.append("error2")
    ... 		bb += 1
    ... 		aa += 1
    ... 
    Traceback (most recent call last):
      File "<interactive input>", line 3, in <module>
    ValueError: invalid literal for float(): n/a
    >>> sub
    [-0.17000000000000004, 'error1']
    >>>
    thanks for all of your help
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Patrick C
    Hey,

    If i use a try/except clause to get around something like a TypeError, will that immediately bust me out of a while loop?

    If so, is there any way i can tell it to stay in the while loop until its completed.

    here's what I'm doing:

    Code:
    >>> stuff2
    ['0.53', '0.36', 'n/a', '0.45', '0.60']
    >>> aa = 0
    >>> bb = 1
    >>> sub = []
    >>> while bb < len(stuff2):
    ... 	try:
    ... 		sub.append(float(stuff2[bb]) - float(stuff2[aa]))
    ... 		bb += 1
    ... 		aa += 1
    ... 		try:
    ... 			sub.append(float(stuff2[bb]) - float(stuff2[aa]))
    ... 			bb += 1
    ... 			aa += 1
    ... 		except ValueError:
    ... 			sub.append("error1")		
    ... 			bb += 1
    ... 			aa += 1
    ... 	except TypeError:		
    ... 		sub.append("error2")
    ... 		bb += 1
    ... 		aa += 1
    ... 
    Traceback (most recent call last):
      File "<interactive input>", line 3, in <module>
    ValueError: invalid literal for float(): n/a
    >>> sub
    [-0.17000000000000004, 'error1']
    >>>
    thanks for all of your help
    I am unsure about the result you are looking for. This code will pass on a ValueError (trying to convert 'n/a' to a float) and break out of the loop on an IndexError (trying to subtract an item that is beyond the length of the list):[code=Python]stuff2 = ['0.53', '0.36', 'n/a', '0.45', '0.60']
    sub = []
    for i, item in enumerate(stuff 2):
    try:
    sub.append(floa t(item) - float(stuff2[i+1]))
    except ValueError:
    pass
    except IndexError:
    break
    print sub [/code]
    Output:
    >>> [0.1700000000000 0004, -0.1499999999999 9997]

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #3
      Originally posted by Patrick C
      Hey,

      If i use a try/except clause to get around something like a TypeError, will that immediately bust me out of a while loop?

      If so, is there any way i can tell it to stay in the while loop until its completed.

      here's what I'm doing:

      Code:
      >>> stuff2
      ['0.53', '0.36', 'n/a', '0.45', '0.60']
      >>> aa = 0
      >>> bb = 1
      >>> sub = []
      >>> while bb < len(stuff2):
      ... 	try:
      ... 		sub.append(float(stuff2[bb]) - float(stuff2[aa]))
      ... 		bb += 1
      ... 		aa += 1
      ... 		try:
      ... 			sub.append(float(stuff2[bb]) - float(stuff2[aa]))
      ... 			bb += 1
      ... 			aa += 1
      ... 		except ValueError:
      ... 			sub.append("error1")		
      ... 			bb += 1
      ... 			aa += 1
      ... 	except TypeError:		
      ... 		sub.append("error2")
      ... 		bb += 1
      ... 		aa += 1
      ... 
      Traceback (most recent call last):
        File "<interactive input>", line 3, in <module>
      ValueError: invalid literal for float(): n/a
      >>> sub
      [-0.17000000000000004, 'error1']
      >>>
      thanks for all of your help
      I think the exceptions that are being raised are ValueErrors so you only need one try/except block that catches a ValueError. However, I think there is an easier to do what you are trying to do:
      [code=python]
      stuff2 = ['0.53', '0.36', 'n/a', '0.45', '0.60']
      sub = []

      for (i, obj) in enumerate(stuff 2):
      try:
      sub.append(floa t(obj) - float(stuff2[i+1]))
      except ValueError:
      sub.append("Err or")
      [/code]
      I'm not sure if that does what you want.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I noticed in your other thread that you want to include the error in the list 'sub'.[code=Python]stuff2 = ['0.53', '0.36', 'n/a', '0.45', '0.60']
        sub = []
        for i, item in enumerate(stuff 2):
        try:
        sub.append(floa t(item) - float(stuff2[i+1]))
        except ValueError, e:
        sub.append(str( e))
        except IndexError:
        break
        print sub [/code]

        >>> [0.1700000000000 0004, 'invalid literal for float(): n/a', 'invalid literal for float(): n/a', -0.1499999999999 9997]

        Comment

        Working...