Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zero Division Error
#1
Hello everyone
This goal of this assignment is to use a separate text file and then use the numbers within it and compute the variance. My professor has barred us from using the variance library function, so we have to do it manually. I believe I'm close I just can't figure out why I'm getting a ZeroDivisionError.
This is what I have so far:
l = []
with open('in3.txt') as f:
    for line in f:
        numstr = line.rstrip().lstrip()
        numstr = int(numstr)
        l.append(numstr)
        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)
print(total3)
the text file is called 'in3' and it reads:

2
3
4
-1
3


Any help is greatly appreciated thanks for looking.
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,624 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  Division problem Eric7Giants 1 1,733 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,348 Apr-20-2019, 07:50 PM
Last Post: ichabod801
  Zero Division Error moga2003 4 3,135 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