473,378 Members | 1,571 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,378 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 4438
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.