Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zero Division Error
#2
Why are you doing this:
    total = 0
    for i in l:
        total = i + total
        mean = total / len(l)
        total2 = 0
        total3 = 0
        for i in l:
            total2 = (i - mean) ** 2
            total3 = total2 + total3
            total3 = total3 / (len(l) - 1)
Each time you do this:
    for line in f:
        numstr = line.rstrip().lstrip()
        numstr = int(numstr)
        l.append(numstr)
Was your intention to load all the values into "l" and then compute variance? That is not what you're doing.

And let's talk about your variable names. One letter variable names are bad, but two of them are particularly bad. l (lower case L) and O (upper case o). These are particularly bad because they look a lot line one and zero.

total, total2 and total3 aren't much better. What are these for? Why do you need so many totals? numstr is a little better. At least there is some indication of what it does (used to convert str to a num).

The answer for your data is 3.7. I computed this using statistics.variance and numpy.var. I know you cannot use these for this exercise, but it is good to know the right answer.
import numpy
import statistics

f = (" 2 \n", "3  \n", "  4\n", "-1\n", "3\n")
numbers = [int(numstr) for numstr in f]

def naive_variance(numbers):
    n = len(numbers)
    sum_num = sum(numbers)
    sum_sqr = sum(number**2 for number in numbers)
    return (sum_sqr - (sum_num**2)/n) / (n-1)

def twopass_variance(numbers):
    # Leave this as an exercise for you
    ...
    return sum_norm_sqr / (n-1)

def numpy_variance(numbers):
    return numpy.var(numbers, ddof=1)

print(naive_variance(numbers),)
print(twopass_variance(numbers))
print(numpy_variance(numbers))
print(statistics.variance(numbers))
Output:
3.7 3.7 3.7 3.7
Some things to note:
I didn't want to make a file, so I made a list of number strings. I added linefeeds and spaces to the strings and still didn't have to use strip to convert these strings to numbers. You probably don't have to either.

It looks like you are using the two-pass algorithm. You can use sum(...) to compute the sum of a list of numbers instead of using a for loop. If you can use a list comprehension you can compute this in one line:
        for i in l:
            total2 = (i - mean) ** 2  # Can you combine these two lines?
            total3 = total2 + total3
            total3 = total3 / (len(l) - 1)  # Are you sure this belongs here?
Reply


Messages In This Thread
Zero Division Error - by Leo - Mar-24-2022, 11:48 PM
RE: Zero Division Error - by deanhystad - Mar-25-2022, 05:00 AM
RE: Zero Division Error - by Leo - Mar-25-2022, 05:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding square roots using long division. jahuja73 10 5,757 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  Division problem Eric7Giants 1 1,760 Nov-16-2019, 05:50 AM
Last Post: ndc85430
  Count how many carpets you need to fill room floor without multiplication/division Ech0ke 1 2,375 Apr-20-2019, 07:50 PM
Last Post: ichabod801
  Zero Division Error moga2003 4 3,195 Mar-07-2019, 02:15 AM
Last Post: moga2003

Forum Jump:

User Panel Messages

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