471,350 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. >>> stuff2
  2. ['0.53', '0.36', 'n/a', '0.45', '0.60']
  3. >>> aa = 0
  4. >>> bb = 1
  5. >>> sub = []
  6. >>> while bb < len(stuff2):
  7. ...     try:
  8. ...         sub.append(float(stuff2[bb]) - float(stuff2[aa]))
  9. ...         bb += 1
  10. ...         aa += 1
  11. ...         try:
  12. ...             sub.append(float(stuff2[bb]) - float(stuff2[aa]))
  13. ...             bb += 1
  14. ...             aa += 1
  15. ...         except ValueError:
  16. ...             sub.append("error1")        
  17. ...             bb += 1
  18. ...             aa += 1
  19. ...     except TypeError:        
  20. ...         sub.append("error2")
  21. ...         bb += 1
  22. ...         aa += 1
  23. ... 
  24. Traceback (most recent call last):
  25.   File "<interactive input>", line 3, in <module>
  26. ValueError: invalid literal for float(): n/a
  27. >>> sub
  28. [-0.17000000000000004, 'error1']
  29. >>> 
thanks for all of your help
Sep 27 '07 #1
3 4336
bvdet
2,851 Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. >>> stuff2
  2. ['0.53', '0.36', 'n/a', '0.45', '0.60']
  3. >>> aa = 0
  4. >>> bb = 1
  5. >>> sub = []
  6. >>> while bb < len(stuff2):
  7. ...     try:
  8. ...         sub.append(float(stuff2[bb]) - float(stuff2[aa]))
  9. ...         bb += 1
  10. ...         aa += 1
  11. ...         try:
  12. ...             sub.append(float(stuff2[bb]) - float(stuff2[aa]))
  13. ...             bb += 1
  14. ...             aa += 1
  15. ...         except ValueError:
  16. ...             sub.append("error1")        
  17. ...             bb += 1
  18. ...             aa += 1
  19. ...     except TypeError:        
  20. ...         sub.append("error2")
  21. ...         bb += 1
  22. ...         aa += 1
  23. ... 
  24. Traceback (most recent call last):
  25.   File "<interactive input>", line 3, in <module>
  26. ValueError: invalid literal for float(): n/a
  27. >>> sub
  28. [-0.17000000000000004, 'error1']
  29. >>> 
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):
Expand|Select|Wrap|Line Numbers
  1. stuff2 = ['0.53', '0.36', 'n/a', '0.45', '0.60']
  2. sub = []
  3. for i, item in enumerate(stuff2):
  4.     try:
  5.         sub.append(float(item) - float(stuff2[i+1]))
  6.     except ValueError:
  7.         pass
  8.     except IndexError:        
  9.         break
  10. print sub    
Output:
>>> [0.17000000000000004, -0.14999999999999997]
Sep 27 '07 #2
ilikepython
844 Expert 512MB
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:

Expand|Select|Wrap|Line Numbers
  1. >>> stuff2
  2. ['0.53', '0.36', 'n/a', '0.45', '0.60']
  3. >>> aa = 0
  4. >>> bb = 1
  5. >>> sub = []
  6. >>> while bb < len(stuff2):
  7. ...     try:
  8. ...         sub.append(float(stuff2[bb]) - float(stuff2[aa]))
  9. ...         bb += 1
  10. ...         aa += 1
  11. ...         try:
  12. ...             sub.append(float(stuff2[bb]) - float(stuff2[aa]))
  13. ...             bb += 1
  14. ...             aa += 1
  15. ...         except ValueError:
  16. ...             sub.append("error1")        
  17. ...             bb += 1
  18. ...             aa += 1
  19. ...     except TypeError:        
  20. ...         sub.append("error2")
  21. ...         bb += 1
  22. ...         aa += 1
  23. ... 
  24. Traceback (most recent call last):
  25.   File "<interactive input>", line 3, in <module>
  26. ValueError: invalid literal for float(): n/a
  27. >>> sub
  28. [-0.17000000000000004, 'error1']
  29. >>> 
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:
Expand|Select|Wrap|Line Numbers
  1. stuff2 = ['0.53', '0.36', 'n/a', '0.45', '0.60']
  2. sub = []
  3.  
  4. for (i, obj) in enumerate(stuff2):
  5.     try:
  6.         sub.append(float(obj) - float(stuff2[i+1]))
  7.     except ValueError:
  8.         sub.append("Error")
  9.  
I'm not sure if that does what you want.
Sep 27 '07 #3
bvdet
2,851 Expert Mod 2GB
I noticed in your other thread that you want to include the error in the list 'sub'.
Expand|Select|Wrap|Line Numbers
  1. stuff2 = ['0.53', '0.36', 'n/a', '0.45', '0.60']
  2. sub = []
  3. for i, item in enumerate(stuff2):
  4.     try:
  5.         sub.append(float(item) - float(stuff2[i+1]))
  6.     except ValueError, e:
  7.         sub.append(str(e))
  8.     except IndexError:        
  9.         break
  10. print sub    
>>> [0.17000000000000004, 'invalid literal for float(): n/a', 'invalid literal for float(): n/a', -0.14999999999999997]
Sep 27 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Kin®sole | last post: by
7 posts views Thread by Robert Brewer | last post: by
12 posts views Thread by AMT2K5 | last post: by
2 posts views Thread by bughunter | last post: by
9 posts views Thread by -Nacho- | last post: by
6 posts views Thread by Shawn Minisall | last post: by
21 posts views Thread by Dan Tallent | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.