beginner's python help

hrishy hrishys at yahoo.co.uk
Sun Sep 6 03:17:18 EDT 2009


Hi

I am just a  python beginner 

What you need is exceptions 
http://docs.python.org/tutorial/errors.html

something on the lines of since you expect a integer and you wnat to catch the exception
...     try:
...         sum = 0;
...           for item in readData:
...            sum += int(item)
...     except ValueError:
...         print "Oops!  That was no valid number.  Try again...",item




--- On Sun, 6/9/09, Maggie <la.foma at gmail.com> wrote:

> From: Maggie <la.foma at gmail.com>
> Subject: beginner's python help
> To: python-list at python.org
> Date: Sunday, 6 September, 2009, 8:00 AM
> code practice:
> 
> test = open ("test.txt", "r")
> readData = test.readlines()
> #set up a sum
> sum = 0;
> for item in readData:
>     sum += int(item)
> print sum
> 
> test file looks something like this:
> 
> 34
> 23
> 124
> 432
> 12
> 
> when i am trying to compile this it gives me the error:
> invalid
> literal for int() with base 10
> 
> i know a lot of people get this and it usually means that
> you try to
> cast a string into an integer and this string does not
> really contain
> a “digit”..so I am just not sure how to correct it in
> this case...
> 
> thanks for your input
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 


      



More information about the Python-list mailing list