Re: beginner's python help

2009-09-07 Thread Niklas Norrthon
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('

Re: beginner's python help

2009-09-07 Thread Esmail
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

Re: beginner's python help

2009-09-06 Thread Terry Reedy
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

Re: beginner's python help

2009-09-06 Thread MRAB
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

Re: beginner's python help

2009-09-06 Thread Benjamin Kaplan
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: >>

Re: beginner's python help

2009-09-06 Thread Chris Rebert
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: >>

Re: beginner's python help

2009-09-06 Thread Maggie
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) > >> >    

Re: beginner's python help

2009-09-06 Thread Chris Rebert
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

Re: beginner's python help

2009-09-06 Thread Maggie
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

Re: beginner's python help

2009-09-06 Thread Chris Rebert
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

Re: beginner's python help

2009-09-06 Thread hrishy
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

Re: beginner's python help

2009-09-06 Thread Chris Rebert
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

Re: beginner's python help

2009-09-06 Thread hrishy
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

Re: beginner's python help

2009-09-06 Thread hrishy
(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

Re: beginner's python help

2009-09-06 Thread Chris Rebert
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

beginner's python help

2009-09-06 Thread 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 b