473,218 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 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 4432
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

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

Similar topics

12
by: Kin®sole | last post by:
Hi I'm very new to VB (using VB6) I have two lists one blank and one containing names in the format of surname and then forename.I also have a combo box containing forenames.When I select a...
7
by: Robert Brewer | last post by:
Alex Martelli wrote in another thread: > One sign that somebody has moved from "Python newbie" to "good Python > programmer" is exactly the moment they realize why it's wrong to code: > > ...
12
by: AMT2K5 | last post by:
Hello. I have the following function titled cleanSpace that recieves a string and cleans it up. My program passes a string to the function via "cleanSpace(c)". The function works the first time...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
14
by: Dixie | last post by:
I am trying to write some code that when I exit my application kills all of the text files in a certain folder. It is possible that some of those files are locked because someone else is using...
9
by: -Nacho- | last post by:
Hi all I have a table with some email addresses of a mailing list subscribers. I'm triying to send a mail to them by querying the table and sending the mail from a 'while' loop, but I only get...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
6
by: Shawn Minisall | last post by:
I've been having some problems with using a while statement for one menu within another while statement for the main menu, first time I've done it. It's with choice number two from the menu. When...
21
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.