beginner's python help

M

Maggie

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
 
C

Chris Rebert

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

A slightly better way to write this:

test = open("test.txt", "r")
#set up a sum
the_sum = 0 #avoid shadowing the built-in function sum()
for line in test: #iterate over the file directly instead of reading
it into a list
the_sum += int(line)
print the_sum
test file looks something like this:

34
23
124
432
12

when i am trying to compile

No, the error is happening at runtime. Pretty much only SyntaxErrors
occur at compile-time.
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...

I would recommend putting a `print repr(line)` inside the loop, before
the "+=" line. This will show the input int() is getting so you can
see out what the bad input is that's causing the error and thus debug
the problem.

Cheers,
Chris
 
T

Terry Reedy

Maggie said:
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...

You already have your specific answer, but you need a general strategy
also. When you have a problem processing data from a file, you should
ask: "Is the problem with the file data? Or is it with the subsequent
processing?". The answers come from two different test programs.

For the first:

print readData
#or
for item in readData: print repr(item)

This would have shown you that the file is not what you thought.

For the second:

readData = ['34', '23'] # etc
#read of program

and it that works, add '\n' to the end of each item.

Any decent programming editor will let you comment out blocks of text
without deleting them.

In other words, to debug, run simple experiments.

Terry Jan Reedy
 
E

Esmail

Maggie said:
code practice:

test = open ("test.txt", "r")
readData = test.readlines()
#set up a sum
sum = 0;

Hi Maggie,

I see you have already gotten a lot of useful help.
One additional suggestion would be to use a different
variable name other than 'sum' as sum is a Python
built-in function.

Cheers,
Esmail
 
N

Niklas Norrthon

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

Assuming the test file looks like above, and isn't stored in rtf
format or something like that. (Open the test file in the python
editor to check its contents).

/Niklas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top