简体   繁体   中英

List as argument in Python 2.7

I am confused as to why the following code is not working. My confusion lies with the returns and prints. I don't know how to make the list as the argument. Everything else seems to work fine up until that part.

def while_mean(l):
  sum = 0
  length = float(len(l))
  index = 0 
  while index < len(l):
    sum = l[index] + sum
    index = index + 1
  return sum/length

def for_mean(l):
  sum = 0
  length = float(len(l))
  for i in l:
    sum = i + sum
    mean = sum/length
  return mean

def stats():
  apd = ([])
  nume = input("write a number:")
  while nume != 'n':
    apd.append(nume)
    print apd
    nume = input("write another number:")
  return sum(apd)
  return while_mean([apd]) 

print stats()
print while_mean([apd])

changed def stats to:

def stats():
  apd = ([])
  nume = raw_input("write a number:")

  while nume != 'n':
    apd.append(float(nume))
    print apd
    nume = raw_input("write another number:")

  return sum(apd)

print apd = ([])
print stats()
print while_mean(apd)

You cannot return twice, so only the first of these two will run

return sum(apd)
return while_mean([apd])

apd is only defined inside the stats function, so if you want to use it outside then define it before running the function or return it.

Also, if you intend apd to be a list then don't call it like while_mean([apd]) but rather

while_mean(apd)

otherwise you create a list of list.

def while_mean(l):
    sum = 0
    length = float(len(l))
    index = 0 
    while index < len(l):
        sum = l[index] + sum
        index = index + 1
    return sum/length

def for_mean(l):
    sum = 0
    length = float(len(l))
    for i in l:
        sum = i + sum
        mean = sum/length
    return mean

def stats():
    nume = input("write a number:")
    while nume != -1:
        apd.append(nume)
        print apd
        nume = input("write another number:")
    return sum(apd)
    return while_mean([apd]) 

apd = ([])
print stats()
print while_mean(apd)

With the way that your code was, you were declaring the apd variable inside of the stats function, but it is better to declare that variable outside of the function, and then pass it into the function as a parameter, so that the current list is maintained outside of the function.
If you do this you can also pass the apd variable to the while_mean function.

Another thing, is in the stats() function, it is better to compare the value of nume to -1 (or any other number you would like) as opposed to a character for the condition of the while loop.
This is because nume is assigned to be an integer , when an integer is entered. So, nume is of the type integer , and because nume is of the type integer , you cannot then enter a character, and have a character assigned to the variable nume .

With this solution you would enter -1 when you are done entering numbers to put in the list.

I have modified your code, and the revisions that I made are above. I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM