Chris Scrivner
Chris Scrivner

Reputation: 1

Why do i have to enter the numbers twice in this code

def numList(): 
    values = [] 
    while True: 
         numbers = int(input("Enter any amount of numbers or -9999 to quit: "))
         if numbers == -9999:break
         values.append(numbers)

    return values



def AvgAllNum(values): 
    allNum = [] 
    average = 0 
    for i in values: 
         allNum.append(i) 
    average = sum(allNum)/len(allNum) 
    return average

def AvgPositive(values): 
    posNum = [] 
    average = 0 
    for i in values: 
         if i > 0: 
              posNum.append(i) 
              average = sum(posNum)/len(posNum) 
    return average

def AvgNonPos(values): 
    nonNum = [] 
    average = 0 
    for i in values:
        if i < 1: 
              nonNum.append(i) 
              average = sum(nonNum)/len(nonNum) 
    return average

def dictionary():

     return {'all': AvgAllNum(), 'pos': AvgPositive(), 'def': AvgNonPos()}() 

print("Your list of numbers are", numList())    
print("Enter your numbers again to get the averages and -9999 to return averages")




values = numList()


print("Your Averages Are")    
print('allNum', AvgAllNum(values), 'posNum', AvgPositive(values), 'NonNum', AvgNonPos(values))

Upvotes: 0

Views: 48

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90989

The is because first you are calling numList() in the print line as -

print("Your list of numbers are", numList()) 

Then you are again calling it here -

values = numList()

So you are asking for the numbers twice, though only the second time, you actually save the numbers inputted. The first time you just print them and throw them away.

Move the print after the values = numList() call and print values instead. Example -

print("Enter your numbers again to get the averages and -9999 to return averages")

values = numList()
print("Your list of numbers are", values)    

print("Your Averages Are")    
print('allNum', AvgAllNum(values), 'posNum', AvgPositive(values), 'NonNum', AvgNonPos(values))

Upvotes: 2

Related Questions