Python Forum

Full Version: Python won 't do decimals
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
It took me a lot of tries before I was able to figure out the problem, and I was stunned. When building my genealogy website, I display 100 entries per page, but I don't want final pages with only a small handful. If there remain 101 entries I'd rather display 51 on one page and 50 on the next. It should have been simple with

z = (remainder / 2)
z = int(z + 0.6)
However, it makes things worse. It takes the first line and instead of z = 50.5 it says z = 50 and the next line doesn't help. The program puts 50 lines on one page, 50 on the next and then builds a page with one lone entry. I had to work around and say

if remainder % 2 == 1:
     z = z + 1
I don't recall this happening before in the years I've been coding in Python.

What is the way to say I absolutely want a decimal answer to remainder / 2 and also not a string variable because I need to do more math with it ?
In Python 3, 101 / 2 returns 50.5. Are your running Python 2? That does integer division if both numbers are ints. If that is the case, divide by 2.0 and you will get a float. But why would you be using Python 2?

If it is not Python 2, maybe this is not Python, but some python-like thing. You mention this is for a website. What tools are you using for the website?
It looks like you are using python2

Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 5/2
2.5
I'm running Python 3 using Terminal on my Mac. I type in

python example.py at the command prompt.
Please show the output of python -V and the contents of example.py. I cant run your first example because remainder isn't assigned.
(Jun-08-2023, 03:25 PM)SomebodySmart Wrote: [ -> ]I'm running Python 3 using Terminal on my Mac.

I doubt it. Check https://peps.python.org/pep-0238/
and
https://docs.python.org/3/using/mac.html#

Probably you have python2 preinstalled and python refers to that one.