Ex4.py Learn Python 3 The Hard Way 2017 Floating point

Hi, Zed! Again I try to learn Python by your book. I’ve just made exercise 4 Variables and Names. I have a question on which I could not find an answer. It is all about a floating point.
In the original version of the exercise, after running we have two lines with floating points “We can transport 120.0 people today.” and “We need to put about 3.0 in each car.”.
According to paragraph 1 of Study Drills, I replaced 4.0 on 4.
cars = 100
space_in_a_car = 4
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print(“There are”, cars, “cars available.”)
print(“There are only”, drivers, “drivers available.”)
print(“There will be”, cars_not_driven, “empty cars today.”)
print(“We can transport”, carpool_capacity, “people today.”)
print(“We have”, passengers, “to carpool today.”)
print(“We need to put about”, average_passengers_per_car, “in each car.”)

After that, we have the very lines “We can transport 120 people today.” and “We need to put about 3.0 in each car.”
The first one is without the floating point and the second is with it. Why??? All variables have numbers without a floating point except “average_passengers_per_car”!
I even typed an additional line “print(cars, space_in_a_car, cars_driven, passengers, average_passengers_per_car, carpool_capacity)” but the result is the same “100 4 30 90 3.0 120”.
I’ve looked through Python topics but I cannot find that anybody asks the same question.
I try my best but I cannot get it. Please help me understand that!

Hmm, ok so you can force any of these to be float or int with this:




That will then make them be exactly what you want. The other thing is you can do // in Python 3 to force integer division so try that.

Also, you can play with this in Python. Type:




And it’ll give you a “shell” to type python code and get an immediate response. Try that. Then you can quit with: quit()

I have tried // to force integer division and it has helped.
int() works too.
python3 returns an error in PowerShell on Windows 10.
Thank you, Zed. :ok_hand:
P.S. It is a strange bug. :confused:

The python3 command is only something that people on maybe Linux or OSX might need to use. If you get an error then you don’t need it.

Yes. I mean exactly the same. And now I use int() in ex5.py so well because of a floating point in that exercise.
See you in ex5.py. I have something there.:wink:

I prefer int() over // because they changed what // means in Python 2 and Python 3. If you get used to // but try to use Python 2 you’ll have weird errors, so int keeps you out of trouble.

Hi again! I found in Python doc if we do / (divide) the result will always return the float point.This is how it works in Python.

I was referring to this change:

Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 / 3
>>> 1 // 3
zedshaw@DESKTOP-8F7E1MF:~$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 / 3
>>> 1 // 3