def getAverage(numList):
total = 0
for i in range(0, len(numList)):
total = float(numList)
total = total + numList
average = total/len(numList)

return average


Hi, I'm really, really new to python, I'm having error on this function and I can't solve it yet, so can someone help? Thank-you :)

Recommended Answers

All 13 Replies

Use code tags.
A shorter way to average.

from __future__ import division

def getAverage(numList):
    return sum(numList)/len(numList)

print getAverage([1,2,3,4])

@snippsat

Thank-you, but can you please will this work if i'm reading the file from a list? My project requires me to create a def function that can convert the list of numbers into a number before I can get the average. Btw am I able to put anything in the brackets at the last line you've indicated? Sorry for being so noob :(

@snippsat

Thank-you, but can you please will this work if i'm reading the file from a list? My project requires me to create a def function that can convert the list of numbers into a number before I can get the average. Btw am I able to put anything in the brackets at the last line you've indicated? Sorry for being so noob :(

"...reading the file from a list?" You mean reading a file into a list? If so, please post the contents of the file between [CODE=text] [/code] tags so we can see how many numbers per line, what character occurs between the numbers (space, tab, comma, etc.) and what other characters are in the file besides numbers.

By "...convert the list of numbers into a number before I can get the average" do you mean add the list of numbers to get a total? That's what sum(numList) does in snippsat's function.

total = float(numList[i])
        total = total + numList[i]

What are you trying to do, obviously numList is a strList. You are trying to double the value of numList? why not

total = 2 * float(numList[i])

@d5e5,

Okay I didn't want to give the whole project that i had to try and do, but it is obvious I'm too dum to learn even python. I really appreciate your help and others. This is the file (list) i have from school exactly like this.

60,70,66
85,74,80
83,91,72
75,62,72

My project is to define two function, 1. to convert it to a number then 2. to get the average. Can anyone please show me how the definition should be in the most simplest way. So, I'll have a total of 3 programs. the main will call the the two functions within the program.
@tonyjvk,
Thank-you :)

Btw, I am really new to python, meaning dum new, but I want to learn thou :), I just need some guidance and help from all the pros that know how. I REALLY APPRECIATE ALL YOUR HELP :)

but it is obvious I'm too dum to learn

or to use a spell-checker. Converting from one data type to another is called "type casting", so to an integer would use int().

str_list = ["1", "3", "5"]
for x in str_list:
   print x, type(x)
print
int_list = [int(x) for x in str_list]
for x in int_list:
   print x, type(x)

Hope this leaves something to you to do but helps you to get started, I left out the file input part with looping.

def avg(seq):
    """divide sum of sequence by it's length to get average, make sure
       Python 2 does not use integer division by making float the sum
    """
    return float(sum(seq))/len(seq)

line_of_numbers = "60,70,66"
# get numbers separated with , as number to list numbers
numbers = [float(number_string) ## change to real float numbers
           for number_string in line_of_numbers.split(',')]

print ('Sum of numbers %s is %.2f and they are %i, that makes average %.2f' %
       (numbers, sum(numbers), len(numbers), avg(numbers)))

@woooee, tonyjv,

Thank-for trying to help me, but I think I'm more lost now lol. I think I have to really search for the dum version of how to do python ====> me :(.

@woooee, tonyjv,

Thank-for trying to help me, but I think I'm more lost now lol. I think I have to really search for the dum version of how to do python ====> me :(.

Could you understand this version which does not save the numbers to list, easier:

def avg(total,count):
    return float(total)/count

line_of_numbers = "60,70,66"
# get numbers separated with , as number to list numbers
total, count = 0.0, 0
for number in line_of_numbers.split(','):
    print number # debug print
    total += float(number)
    count += 1 
print 'Total:', total, 'count', count
print 'Average:', avg(total,count)

@tonyjv,
Omg, I understand it much more now :). One question thou, on line number 4 do I import my list of numbers here? I guess would I just do...

infile = open("files goes here","r")

Thank - you :), I love you lol

@tonyjv,

I love you!! I understand it much more now :) Btw, do i just add my list of numbers on line number 4? Like this?

infile = open("files name goes here","r"
lines = infile.readlines()
infile.close

I'm sorry to take up your time with so much noob question. I do appreciate the time you take to teach a dumbass like me :)

with open('README.txt') as mydata:
    for line in mydata:
        print line,

Is the current prefered way to open (and close automatically, even in case of error) a file. line includes newline character.

@tonyjv,

Thank-you :) You made my day.

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.