Python Forum
Error is finding mean of a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error is finding mean of a list (/thread-40704.html)



Error is finding mean of a list - PythonBoy - Sep-10-2023

So I was making a program to find the mean of a list entered by the user
My code:
Input =input("Please enter the data: ")
Data = Input.split(",")

Data1 = []
Length = len(Data1)
Sum = sum(Data1)
for x in Data:
    Data1.append(int(x))

Mean = Sum/Length
print("The mean of ",Data1, "is ",Mean)
First I asked the user for data in this format(1,2,7,89)
And then I separated all of them using split()
Then I converted every element of the list into integer and added it into another list
Then I found the mean

Error:
Please enter the data: 1,2 Traceback (most recent call last): Mean = Sum/Length ~~~^~~~~~~ ZeroDivisionError: division by zero



RE: Error is finding mean of a list - perfringo - Sep-10-2023

Python interactive interpreter is your best friend.

>>> a = []
>>> len(a)
0                               
>>> # source of ZeroDivsionError
>>> data = input('Please enter integers separated by comma: ').split(',')
Please enter integers separated by comma: 1,2,3
>>> data
['1', '2', '3']
>>> sum(int(i) for i in data)/len(data)
2.0                          
>>> # returns float
>>> import statistics.  
>>> # built-in module
>>> statistics.mean(int(i) for i in data)
2                          
>>> # returns int
I don't recommend to use capitalized built-ins as names.


RE: Error is finding mean of a list - PythonBoy - Sep-10-2023

(Sep-10-2023, 08:53 AM)perfringo Wrote: Python interactive interpreter is your best friend.

>>> a = []
>>> len(a)
0                               
>>> # source of ZeroDivsionError
>>> data = input('Please enter integers separated by comma: ').split(',')
Please enter integers separated by comma: 1,2,3
>>> data
['1', '2', '3']
>>> sum(int(i) for i in data)/len(data)
2.0                          
>>> # returns float
>>> import statistics.  
>>> # built-in module
>>> statistics.mean(int(i) for i in data)
2                          
>>> # returns int
I don't recommend to use capitalized built-ins as names.

Okkkkk..., now I understood.
Data1 = []
Length = len(Data1)
Sum = sum(Data1)
for x in Data:
    Data1.append(int(x))

Mean = Sum/Length
print("The mean of ",Data1, "is ",Mean)
If you see here in my code, I made the variables of Length and Sum before adding integers to the list Data1.
So this is why there is an error.

Thanks so much for helping me out! Dance Tongue


RE: Error is finding mean of a list - perfringo - Sep-11-2023

Length of the Data1 is zero at the time of assignment. If you move this row after the for loop you solve the ZeroDivisionError.

However, Sum have the same problem and should be fixed as well.


RE: Error is finding mean of a list - PythonBoy - Sep-11-2023

(Sep-11-2023, 04:57 AM)perfringo Wrote: Length of the Data1 is zero at the time of assignment. If you move this row after the for loop you solve the ZeroDivisionError.

However, Sum have the same problem and should be fixed as well.

Yes, I noticed it right away and fixed it

Thanks!