# My very first python program, need help

Discussion in 'Python' started by WP, Aug 10, 2008.

1. ### WPGuest

Hello, below is my very first python program. I have some questions
regarding it and would like comments in general. I won't be able to get
my hands on a good python book until tomorrow at the earliest. The
program takes a string and sums all numbers inside it. I'm testing with
the following string: "123xx,22! p1" which should yield a sum of 123 +
22 + 1 = 146. My solution to this exercise was to write a function that
takes a string and splits it into a list of numbers and then I will sum
up the numbers in the list. In fact, I wrote two versions of this
function: One uses a for loop to replace all characters that is not a
digit and not a space with a space. That should leave me with the
numbers intact and with only spaces separating these numbers. Then I
call split to get a list of numbers (here I discovered I must not use a
sep equal to ' ' or the returned list would contain empty strings as
well). This is calculate_sum_1() and look at the comment in the code to
see what worries me there. The second one uses regular expressions and
the problem I have with that is that I get a list of containing not just
the numbers but empty strings as well. I'm working around that in my
loop that sums the numbers but it would be better to not have these
empty string in the list in the first place. Here's the code:

import re

def calculate_sum_1(str):
for c in str:
if c.isdigit() == False and c != ' ':
# That we assign to the variable we're looping over worries
me...
str = str.replace(c, ' ')

mylist = str.split()

print "(for loop version) mylist after replace() and split() = ",
mylist

sum = 0

for item in mylist:
sum += long(item)

return sum

def calculate_sum_2(str):
#print "In replace_nondigits_2(), str = ", str
p = re.compile('[^0-9]')

mylist = p.split(str)

print "(regex version) mylist after calling split(): ", mylist

sum = 0

for item in mylist:
if item.isdigit():
sum += long(item)

return sum

str = "123xx,22! p1" # Sum = 123 + 22 + 1 = 146

print "str = ", str

print "calculate_sum_1(str): %d" % calculate_sum_1(str)
print "calculate_sum_2(str): %d" % calculate_sum_2(str)

The output when run is:
str = 123xx,22! p1
(for loop version) mylist after replace() and split() = ['123', '22', '1']
calculate_sum_1(str): 146
(regex version) mylist after calling split(): ['123', '', '', '22', '',
'', '1']
calculate_sum_2(str): 146

Hope I made some sense and thanks for reading!

- Eric (WP)

WP, Aug 10, 2008

2. ### WPGuest

Wojtek Walczak wrote:
[snip]
Thanks for all your help. I've incorporated your suggestions and moved
on to my next program. See new thread.

- Eric (WP)

WP, Aug 10, 2008

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 (here). After that, you can post your question and our members will help you out.