473,408 Members | 1,700 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,408 software developers and data experts.

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...

Expand|Select|Wrap|Line Numbers
  1. results = []
  2. while second < len(xx):
  3.     first = 0
  4.     second = 1
  5.     results.append(xx[second] - xx[first])
  6.     first += 1
  7.     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...
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "<interactive input>", line 4, in <module>
  3. ValueError: invalid literal for float(): n/a
Any ideas how i could get around that? Thanks

PC
Sep 26 '07 #1
3 1292
ilikepython
844 Expert 512MB
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...

Expand|Select|Wrap|Line Numbers
  1. results = []
  2. while second < len(xx):
  3.     first = 0
  4.     second = 1
  5.     results.append(xx[second] - xx[first])
  6.     first += 1
  7.     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...
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "<interactive input>", line 4, in <module>
  3. 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:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     # code that might raise an exception here
  3. except ValueError:
  4.     # code the runs if the specified exception is raised
  5.  
  6. # program resumes
  7.  
Also, you while loop seems wrong.
Sep 26 '07 #2
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
Expand|Select|Wrap|Line Numbers
  1. >>> stuff2 = ['0.53', '0.36', 'n/a', '0.45', '0.60']
  2. >>> bb = 1
  3. >>> aa = 0
  4. >>> sub = []
  5. >>> sub
  6. []
  7. >>> while bb < len(stuff2):
  8. ...     try:
  9. ...         sub.append(stuff2[bb] - stuff2[aa])
  10. ...         bb += 1
  11. ...         aa += 1
  12. ...         try:
  13. ...             sub.append(stuff2[bb] - stuff2[aa])
  14. ...             bb += 1
  15. ...             aa += 1
  16. ...         except ValueError:
  17. ...             sub.append("error1")        
  18. ...             bb += 1
  19. ...             aa += 1
  20. ...     except TypeError:        
  21. ...         sub.append("error2")
  22. ...         bb += 1
  23. ...         aa += 1
  24. ... 
  25. >>> sub
  26. ['error2', 'error2', 'error2', 'error2']
Sep 27 '07 #3
Ignore that, i forgot that stuff2 is still a string, if i still am having a problem, i'll put another post up.

thanks
Sep 27 '07 #4

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

Similar topics

1
by: Kamilche | last post by:
I'm totally new to Python error handling. I see there are many, many ways to handle errors in Python. What is the standard accepted 'best practice'? I will be writing unit tests for every...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
3
by: Gonçalo Rodrigues | last post by:
Hi all, I've got two somewhat general questions related to exception handling in C++. My usual programming language is the lovely and flexible Python, but for various reasons, including the need...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
2
by: Petr Jakes | last post by:
I am a little bit confused by all possibilities for exceptions handling in Python (probably because I am not skilled enough??) I did try to search trough this list and reading Python tutorial about...
5
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored...
3
by: Wolfram | last post by:
I have a problem with displaying errors in an embedded situation. The "main program" I want to embed Python into is a windows, MFC, non-console, C++ application. My issue is that I have not been...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
2
by: Omar Abid | last post by:
Reason of this project: Error handling is one of the most difficult thing that may afford a programmer. It isn't as easy as you think and handling errors in a program some time can make errors...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.