I am working on a homework project and i have it all done but the beginning and i can't figure out why i keep getting either errors or weird outputs. the following is the snippet that i am having trouble with.


It keeps giving me an index error or when i change the parameters to the index it gives me the wrong output.

def line_sum_and_count(line):
    """Finds the sum and count of the numbers in the given line of text."""
    #-----------------------------------------------------
    
    split = string.split(line," ")
    count = len(split) 
    for i in range(len(split)):
        split[i] = float(split[i])
    for n in range(len(split)):
        sum = split[n] + split[n+1] 
    #-------------------------------------------------------    
    
    return sum, count

def test_line_sum_and_count():
    """Tester for the above function """
    sum, count = line_sum_and_count('14.0 20.0 17') 
    if sum == 51 and count == 3:
        print "passed"
    else:
        print "failed"

Put your code in code tags my friend! That [#CODE = python]
Your code from editor/IDE is pasted here
[#/CODE] Remove harsh in Tags

The reason it will say failed at the end of the code is becuase sum and count only exist inside your method. To stop that you can add global sum and global count to the start of the code.

wait im wrong. Sorry didnt read it properly!
But i did have a quick go and this works:

def line_sum_and_count(line):
    """Finds the sum and count of the numbers in the given line of text."""
    #-----------------------------------------------------
    
    split = line.split()
    count = 0
    sum=0
    for f in split:
        count+=1
        sum += float(f)
    #-------------------------------------------------------    
    
    return sum, count

def test_line_sum_and_count():
    """Tester for the above function """
    sum, count = line_sum_and_count('14.0 20.0 17') 
    if sum == 51 and count == 3:
        print "passed"
    else:
        print "failed" 
test_line_sum_and_count()

Do not use sum as a variable name, because it is a built in function. I would suggest that you not use split either, because it is confusing (it is to me anyway). Use sum() and len() to return your values.

def line_sum_and_count(line):
    """Finds the sum and count of the numbers in the given line of text."""
    words = line.split()
    return sum([float(s) for s in words]), len(words)
def line_sum_and_count(line):
    """Finds the sum and count of the numbers in the given line of text."""
    #-----------------------------------------------------
    
    data = line.split()
    tally = 0
    for i in data:
        tally += float( i )
    #-------------------------------------------------------    
    
    return tally, len( data )

Here's a different way to tackle the first function. I think your problem was the whole [n] + [n+1] thing... So that would mean
Iteration 1: 14 + 20
Iteration 2: 20 + 17
Iteration 3: 17 + Out of Bounds
so not only were you accumulating incorrectly, but you weren't doing anything like sum += x (sum = sum + x ). Do you understand what I mean? An accumulator is best done as the above code, you initialize your accumulator first to 0 (tally), then on each iteration you would just do tally += x (tally = tally + x). I hope that makes sense.

Secondly, it's easier if you just iterate over the list that is created by split(), and use the float conversion dynamically instead of iterating once to convert all the elements to floats, and then a second iteration for the "summing".

Thirdly, I agree that you should avoid using built in functions as variable names. You should consider switching to a Python editor that has syntax highlighting and/or auto-completion, as this will help to identify function names easily.

Finally, don't use the string module. It clutters your code unnecessarily. Instead of string.split(x, d) you can simply use x.split(d), which is what I did in the code above. And if you do not use a delimiter (d) as a parameter it will default to white space (spaces, tabs, newlines).

HTH!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.