# Zero Division Error

Page 1 of 1

## 4 Replies - 386 Views - Last Post: 19 April 2013 - 04:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=318864&amp;s=4662c6c137fb79bd6ab5968139dcf051&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mattsd101

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 18-April 13

# Zero Division Error

Posted 18 April 2013 - 07:20 AM

```import statisticsSolver

def main():
total = 0
counter = 1950
high_temp = []
low_temp = []
high_year = []
low_year = []
temperatures = []
avg = statisticsSolver.mean(temperatures)
stdev = statisticsSolver.stdev(temperatures)
stdev_upper = avg + (2 * stdev)
stdev_lower = avg - (2 * stdev)

temp_file = open("Project2Temps.txt", "r")
for temps in temp_file:
temperatures.append(int(temps))
for year in counter:
year = counter + 1
while total < len(temperatures):
temperatures[total] = temperatures[total].rstrip("\n")
total += 1
for temps in temperatures:
if temps > stdev_upper:
high_temp.append(int(temps))
high_temp.append(int(temps))
for temps in temperatures:
if temps < stdev_lower:
low_temp.append(int(temps))
low_temp.append(int(temps))

temp_average = statisticsSolver.mean(temperatures)
print("This is the average of the tempteratures:", temperatures)
stdev_temperature = statisticsSolver.stdev(temperatures)
print("the Standard deviation is:", stdev_temperature)

main()
```

```import math

def mean(temperatures):
avg_total = 0
for temp in temperatures:
avg_total += int(temp)
average = avg_total / len(temperatures)
return average

def stdev(temperatures):
total = 0
total_variance = 0
for item in temperatures:
total = total + int(item)
avg = total / len(temperatures)
for item in temperatures:
variance = (int(item) - avg)*(int(item) - avg)
total_variance = total_variance + variance
denominator = len(temperatures)  -1
stdev = math.sqrt(total_variance / denominator)
return stdev
```

I am getting this error upon running the program:

Traceback (most recent call last):
File "P:\COSC 118\Project2\Project2.py", line 41, in <module>
main()
File "P:\COSC 118\Project2\Project2.py", line 14, in main
avg = statisticsSolver.mean(temperatures)
File "P:\COSC 118\Project2\statisticsSolver.py", line 10, in mean
average = avg_total / len(temperatures)
ZeroDivisionerror: division by zero

I have searched for the division by zero error, but I don't completely understand how it relates to my code, thanks.

Is This A Good Question/Topic? 0

## Replies To: Zero Division Error

### #2 tlhIn`toq

• Please show what you have already tried when asking a question.

Reputation: 5466
• Posts: 11,742
• Joined: 02-June 10

## Re: Zero Division Error

Posted 18 April 2013 - 07:28 AM

You don't really need to search: The IDE is telling you the line it is on.

You don't understand how it relates to your code?
You do understand that it is a basic concept in math that you can't divide by zero, right? 14 / 0 is not a legal mathematical form

average = avg_total / len(temperatures)

len(temperatures) is calculating out to 0.
Then you are dividing avg_total by that calculated zero

Same with mean(temperatures). It is calculating out to zero.
Was This Post Helpful? 0

### #3 mattsd101

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 18-April 13

## Re: Zero Division Error

Posted 18 April 2013 - 07:29 AM

```denominator = len(temperatures)  -1
stdev = math.sqrt(total_variance / denominator)
```

I get that I cannot divide by zero. The denominator variable is not 0 though, this is what I don't understand.
Was This Post Helpful? 0

### #4 jon.kiparsky

• Pancakes!

Reputation: 7626
• Posts: 12,856
• Joined: 19-March 11

## Re: Zero Division Error

Posted 18 April 2013 - 10:48 AM

```denominator = len(temperatures)  -1
stdev = math.sqrt(total_variance / denominator)

```

This is not the line that's causing the problem. Our Klingon friend is correct in his diagnosis - len(temperatures) is zero.

The next question to ask yourself is, how did temperatures get to be an empty list? From the code you're showing here, it looks like your data file is empty.

Looking at the rest of your code, there are some issues with the structure. What's the point of having that long "main" function? If you want to make your life easier, define a number of smaller functions that each do one thing, and call them in sequence. This will make it a lot easier for you to debug one issue at a time.

Also, you might want to think about this:

```    while total < len(temperatures):
temperatures[total] = temperatures[total].rstrip("\n")
total += 1

```

Try this as a list comprehension:
```temperatures = [item.rstrip() for item in temperatures]

```

You might as well convert to int at this stage as well:

```temperatures = [int (item.rstrip()) for item in temperatures]

```

and this can be included in a load_temperatures function which returns a list of temperatures, armed and ready for action.
Was This Post Helpful? 0

### #5 mattsd101

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 18-April 13

## Re: Zero Division Error

Posted 19 April 2013 - 04:31 AM

Thank you guys for your help, it really put me on the right track. jon.kiparsky, I tried your suggestions about what I could have done more efficiently but I couldn't get them to work because I don't understand python that well yet. Also, I think I had a few lines of code that I didn't need. But, I was able to properly see what was going on and fix it. The program was supposed to put a file into a list, get the list's average and standard deviation. I also had to include + or - 2 standard deviations. Here is what got the code to work for me just in case anyone else might have this issue in the future:
```import statisticsSolver

def main():
high_temp = []
low_temp = []
temperatures = []
temp_file = open("Project2Temps.txt", "r")
for temps in temp_file:
temperatures.append(int(temps))
avg = statisticsSolver.mean(temperatures)
stdev = statisticsSolver.stdev(temperatures)
stdev_upper = avg + (2 * stdev)
stdev_lower = avg - (2 * stdev)
for temps in temperatures:
if temps > stdev_upper:
high_temp.append(int(temps))
for temps in temperatures:
if temps < stdev_lower:
low_temp.append(int(temps))
temp_average = statisticsSolver.mean(temperatures)
print("This is the average of the Tempteratures:", temp_average)
stdev_temperature = statisticsSolver.stdev(temperatures)
print("the Standard deviation is:", stdev_temperature)
print("The average low temperature was:", low_temp)
print("The average high temperature was:", high_temp)
main()
```

```import math

def mean(temperatures):
avg_total = 0
for temps in temperatures:
avg_total += int(temps)
average = avg_total / len(temperatures)
return average

def stdev(temperatures):
total = 0
total_variance = 0
for item in temperatures:
total = total + int(item)
avg = total / len(temperatures)
for item in temperatures:
variance = (int(item) - avg)*(int(item) - avg)
total_variance = total_variance + variance
denominator = len(temperatures)  -1
stdev = math.sqrt(total_variance / denominator)
return stdev
```

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }