I'm brand new to coding and python. Could someone tell me what is wrong with my code?

def getlist():
     lst=[]
     answer1 = raw_input ("Would you like to add a number to create your mean value? y/n")
     if (answer1 == "y"):
          x = input ("Your number:")
          lst = lst + [x]
     return lst

def mean(l):
   return float(sum(l))/len(l)

a=mean(getList())
print a

Recommended Answers

All 7 Replies

What is your python version?
When you run it do you get any errors?

I use python 3.0 and your modifying code works fine:

def getList():
     lst=[]
     answer1 = input ("Would you like to add a number to create your mean value? y/n")
     if (answer1 == "y"):
         while True:
            x = input ("Your number:")
            if not x: break
            lst = lst + [x]
     return lst

def mean(l):
    s = sum([ int(x) for x in l])
    return float(s/len(l))

a=mean(getList())
print (a)

I'm using python 2.6

my code says I haven't defined getlist

you defined getlist() with lowercase letter 'l' and below used it with uppercase letter 'L'. Python is case-sensitive language. :)

I would do like this for your example, it shows the 'mean' when you just press enter with no input.

def getlist():
    lst = []
    x = None
    while x != '':
        x = raw_input('Your number:')
        if x:
            lst.append(int(x))
    return lst

def mean(l):
   return float(sum(l))/len(l)

a = mean(getlist())
print a

Cheers and Happy coding

You have defined getlist. You have not defined getList.

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.