|
|
#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
|
|
|
|
|
|
#2 |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 24,905
Karma: 47303824
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 | |
|
|
|
|
#3 |
|
Well trained by Cats
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 31,343
Karma: 62025226
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 |
|
|
|
|
|
#4 | |
|
Bibliophagist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 48,850
Karma: 174631406
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, 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 05:44 PM. Reason: Added comment about print statements |
|
|
|
|
|
|
#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 | |
|
|
|
|
#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.
|
|
|
|
|
|
#7 | |
|
Well trained by Cats
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 31,343
Karma: 62025226
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
|
|
|
|
|
|
|
#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 05:26 AM |
| Error - division by zero | fanton44 | Calibre | 2 | 10-27-2015 11:59 AM |
| Custom Column with division | Metaxil | Library Management | 5 | 12-15-2014 07:31 PM |
| ePub division in chapters | TheWatt | ePub | 3 | 04-04-2011 05:02 PM |
| Sony Reader division not going anywhere. | boylec | News | 18 | 07-26-2009 10:44 AM |