Python Forum
Error is finding mean of a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error is finding mean of a list
#1
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
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(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!
"He who conquers himself is the mightiest warriors" - Marcus Aurelius
Stay Positive, Work Hard!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding combinations of list of items (30 or so) LynnS 1 885 Jan-25-2023, 02:57 PM
Last Post: deanhystad
Question Finding string in list item jesse68 8 1,894 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  Finding an element in a 1d list in a 2d array lionrocker221 0 1,836 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  capture next block of text after finding error in file kdefilip2 6 3,453 Nov-27-2019, 07:36 PM
Last Post: buran
  Finding MINIMUM number in a random list is not working Mona 5 3,057 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  finding the closest floating point number in a list Skaperen 17 8,310 Sep-19-2019, 10:39 PM
Last Post: Skaperen
  Finding exact phrase in list graham23s 2 2,913 Mar-13-2019, 06:47 PM
Last Post: graham23s
  I need help finding an error in this code I have Tacnoblode 1 2,699 Jul-18-2018, 05:51 AM
Last Post: buran
  finding similar objects in a list DRPR 4 97,533 Jun-22-2018, 10:05 AM
Last Post: volcano63
  Getting error in finding time.time() value in python Lakshana 1 2,505 Jan-11-2018, 07:07 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020