11-01-2019, 04:14 AM | #1 |
Junior Member
Posts: 9
Karma: 10
Join Date: Oct 2019
Device: Kindle
|
Division fails when using an int variable
I'm trying to get an average rating in my plugin and I keep getting a 'divide by zero' error when I do this:
Code:
top = int(sum(rateList)) bottom = int(len(rateList)) averageRating = top/bottom |
11-01-2019, 09:32 AM | #2 |
Grand Sorcerer
Posts: 24,907
Karma: 47303748
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
What is "ratelist"? I assume it is supposed to be a list of numbers. But, with that result, I would assume it is an empty list. Have you checked that it has the values in it you expect?
|
Advert | |
|
11-01-2019, 10:30 AM | #3 |
Well trained by Cats
Posts: 29,809
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
When you make an Int from a small fraction, you could end up with a zero .
You really need to test for that before attempting division |
11-01-2019, 04:40 PM | #4 | |
Bibliophagist
Posts: 35,461
Karma: 145525534
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Forma, Clara HD, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
Quote:
Perhaps a look at how ratelist is being generated and what is being returned? A few print statements to see what is actually being generated? Last edited by DNSB; 11-01-2019 at 04:44 PM. Reason: Added comment about print statements |
|
11-01-2019, 04:54 PM | #5 |
Junior Member
Posts: 9
Karma: 10
Join Date: Oct 2019
Device: Kindle
|
I've tested by printing each statement len(rateList) and sum(rateList) and both give the expected values.
Additionally, I have them casted to int in that example just because that is what I had currently tried doing to ensure it was indeed an int. I've done it without the casting as well to no avail. |
Advert | |
|
11-01-2019, 05:02 PM | #6 |
Junior Member
Posts: 9
Karma: 10
Join Date: Oct 2019
Device: Kindle
|
I figured it out! The division was nested inside a for loop which was generating the rate list. So, on the first iteration, that list WAS empty. I moved it outside of the loop and worked as expected. Thanks for getting my brain turning.
|
11-01-2019, 09:14 PM | #7 | |
Well trained by Cats
Posts: 29,809
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
Quote:
Come to think of it, I chased one many decades ago, where I forgot to initialize the (self typing) variable. First pass, it would bomb with a 'wrong type' error |
|
11-01-2019, 09:36 PM | #8 |
Junior Member
Posts: 9
Karma: 10
Join Date: Oct 2019
Device: Kindle
|
The discussion definitely helped. Sometimes all it takes is bouncing your thoughts off of others to find your goofs!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to name institutions as authors (i.e. NO name division') | Pachuqismo | Library Management | 9 | 11-28-2017 04:26 AM |
Error - division by zero | fanton44 | Calibre | 2 | 10-27-2015 10:59 AM |
Custom Column with division | Metaxil | Library Management | 5 | 12-15-2014 06:31 PM |
ePub division in chapters | TheWatt | ePub | 3 | 04-04-2011 04:02 PM |
Sony Reader division not going anywhere. | boylec | News | 18 | 07-26-2009 09:44 AM |