On 6 Sep, 09:00, Maggie wrote:
> 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
>
>>> sum(map(int, open('
Maggie wrote:
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 functio
Maggie wrote:
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
Chris Rebert wrote:
On Sun, Sep 6, 2009 at 1:28 AM, Maggie wrote:
On Sep 6, 4:19 am, Chris Rebert wrote:
On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote:
On Sep 6, 3:58 am, Chris Rebert wrote:
On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote:
Hi
sum = 0
for item in readData:
try:
su
On Sun, Sep 6, 2009 at 4:28 AM, Maggie wrote:
> On Sep 6, 4:19 am, Chris Rebert wrote:
>> On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote:
>> > On Sep 6, 3:58 am, Chris Rebert wrote:
>> >> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote:
>> >> > Hi
>>
>> >> > sum = 0
>> >> > for item in readData:
>>
On Sun, Sep 6, 2009 at 1:28 AM, Maggie wrote:
> On Sep 6, 4:19 am, Chris Rebert wrote:
>> On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote:
>> > On Sep 6, 3:58 am, Chris Rebert wrote:
>> >> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote:
>> >> > Hi
>>
>> >> > sum = 0
>> >> > for item in readData:
>>
On Sep 6, 4:19 am, Chris Rebert wrote:
> On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote:
> > On Sep 6, 3:58 am, Chris Rebert wrote:
> >> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote:
> >> > Hi
>
> >> > sum = 0
> >> > for item in readData:
> >> > try:
> >> > sum += int(item)
> >> >
On Sun, Sep 6, 2009 at 1:10 AM, Maggie wrote:
> On Sep 6, 3:58 am, Chris Rebert wrote:
>> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote:
>> > Hi
>>
>> > sum = 0
>> > for item in readData:
>> > try:
>> > sum += int(item)
>> > except ValueError:
>> > print "Oops! That was n
On Sep 6, 3:58 am, Chris Rebert wrote:
> On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote:
> > Hi
>
> > sum = 0
> > for item in readData:
> > try:
> > sum += int(item)
> > except ValueError:
> > print "Oops! That was no valid number. Instead it was:", item
>
> > So you mean
On Sun, Sep 6, 2009 at 12:54 AM, hrishy wrote:
> Hi
>
> sum = 0
> for item in readData:
> try:
> sum += int(item)
> except ValueError:
> print "Oops! That was no valid number. Instead it was:", item
>
> So you mean to say this would ignore the bad data and continue process
Hi
sum = 0
for item in readData:
try:
sum += int(item)
except ValueError:
print "Oops! That was no valid number. Instead it was:", item
So you mean to say this would ignore the bad data and continue processing ?
regards
--
http://mail.python.org/mailman/li
On Sun, Sep 6, 2009 at 12:46 AM, hrishy wrote:
> Hi Chris
>
> What if i want to log that bad data and continue processing is there a way to
> do that ?
Tighten the area included in the try...except:
sum = 0
for item in readData:
try:
sum += int(item)
except ValueError:
pr
Hi Chris
What if i want to log that bad data and continue processing is there a way to
do that ?
regards
--- On Sun, 6/9/09, Chris Rebert wrote:
> From: Chris Rebert
> Subject: Re: beginner's python help
> To: "Maggie"
> Cc: python-list@python.org
> Date: S
(item)
... except ValueError:
... print "Oops! That was no valid number. Try again...",item
--- On Sun, 6/9/09, Maggie wrote:
> From: Maggie
> Subject: beginner's python help
> To: python-list@python.org
> Date: Sunday, 6 September, 2009, 8:00 AM
> c
On Sun, Sep 6, 2009 at 12:00 AM, Maggie wrote:
> 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
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 b
16 matches
Mail list logo