1

I must make a program where the user inputs several numbers and inputs a specific number to stop the inputting process and receive an average. It should detect that the user inputs specifically 1234567 and then display the average but it does not. How can i fix this?

def averages():
    def Average(lst): 
        try:
            return sum(lst)/len(lst)
        except ZeroDivisionError:
            return 0
    nums=[]
    while 1234567 not in nums:
        nums.append(input("Please input numbers and type 1234567 when you wish to find the average if the numbers inputted"))
    mean=Average(nums)
    print(nums)
    print(mean)

I expected it to detect the input 1234567 and then output the average of all the numbers that have been inputted. But when 1234567 is inputted it continues to ask for numbers.

Smack Alpha
  • 1,828
  • 1
  • 17
  • 37
Lynianore
  • 11
  • 1
  • 6
    you need to convert your input (which are `str`ings) to `int`egers. e.g. `nums.append(int(input("Please...")))` this may also help: https://stackoverflow.com/questions/16488278/how-to-check-if-a-variable-is-an-integer-or-a-string/16488383#16488383 – hiro protagonist Oct 22 '19 at 13:03
  • 1
    Note that even with adding `int()`, you would be calculating an average *including the 1234567 value*. You should probably check for your ending value before adding it to the list. – jasonharper Oct 22 '19 at 13:08

5 Answers5

2

In Python 3.xx input() reads input as str which would not match with the integer 1234567, since 1234567 != "1234567"

Instead type cast the input to int. e.g.:

int(input("Please input numbers and type 1234567 when you wish to find the average if the numbers inputted"))
Nader Alexan
  • 2,127
  • 22
  • 36
2

First of all you need to parse your input from str to int. And secondly, although I disagree with this whole technique you should remove 1234567 from the sum and reduce the list elements by 1 to have a correct result. for example:

def averages():
    def Average(lst):
        try:
            return (sum(lst)-1234567)/(len(lst)-1)
        except ZeroDivisionError:
            return 0
    nums=[]
    while 1234567 not in nums:
        nums.append(int(input("Please input numbers and type 1234567 when you wish to find the average if the numbers inputted")))
    mean=Average(nums)
    print(nums)
    print(mean)

averages()
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
1

There are two issues with the code you are trying , below fixes both of them

def averages():
    def Average(lst):
        try:
            return sum(lst)/len(lst)
        except ZeroDivisionError:
            return 0
    nums=[]
    list =[1,2,3,4,5,6,7]
    while 1==1:
        input_num = (int(input("Please input numbers and type 1234567 when you wish to find the average if the numbers inputted")))
        if input_num in list:
            nums.append(input_num)
        else:
            break

    mean=Average(nums)
    print(nums)
    print(mean)

if __name__ == '__main__':
    averages()
Infinite
  • 704
  • 8
  • 27
1
nums=[]
while True:
    response = input("Please input numbers and type 1234567 when you wish to find the average if the numbers inputted: ")
    try:
        response_int = int(response)
        nums.append(response_int)
        if response_int == 123456:
            break
    except:
        print('Not a number, please enter a number')

avg = sum(nums)/len(nums)
print(nums)
print(avg)

It looks like you were not converting your input to an integer. You also don't need to create the function to make this work.

Brandon Meyer
  • 98
  • 1
  • 1
  • 7
0
from statistics import mean

def average(lst):
    lst = [float(x) for x in lst]
    return mean(lst)


if __name__ == '__main__':
    nums = []
    run = True
    while run:
        new = int(input("Please input numbers and type 1234567 when you wish to find the average if the numbers inputted\n"))
        if new == 1234567:
            run = False
        else:
            nums.append(new)
    print(nums)
    print(average(nums))
Frank
  • 1,959
  • 12
  • 27