Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does he uses a floating point in this example?

I am a beginner learning Python from Learn Python the hard way. It's my first programming language that I learn and I am stuck at an exercise.

Exercise: "Explain why the 4.0 is used instead of just 4."

cars = 100
space_in_a_car = 4.0 #Why does he uses 4.0 instead of 4 here?
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."

I honestly can't find any reason why he would use a floating point at line 2 other than to to serve as an example that if I have a floating point number it affects the rest of the expression evaluation(cars_driven * space_in_a_car) resulting in 120.0.

Am I missing something?

like image 373
0101amt Avatar asked Jun 15 '11 15:06

0101amt


2 Answers

This was a simple question with a simple answer that I over-complicated for some reason.

(Assuming that you know why 3/4 returns 0 and why 3/4.0 returns 0.75)

I took a look at the book and the code is only that bit, it doesn't seem to have any more to it, and it does ask:

Explain why the 4.0 is used instead of just 4.

It turns out this is a strange question since there is actually no reason for it. David Heffernan was right all along.

So, when you add the .0, it doesn't have any effect than turning the carpool capacity in a float since you just do:

carpool_capacity = cars_driven * space_in_a_car

I can't understand what the author was aiming for, the only notable difference is again that one prints 120.0 and the other 120

As I pointed out before:

average_passengers_per_car = passengers / float(cars_driven)  #added float

Would make (more) sense since, for example if the passengers = 93 in the original code the average would be 3 and not 3.1 that would be in my opinion more reasonable for an average.

Sorry for the confusion, I hope I got it right now :) and that it helps!

OLD:

The reason probably this:

3/4 # returns 0

That is because int/int == int, so, 4 "fits" 0 times in 3, and no decimal point because it is an int.

You should do:

3/4. # returns 0.75

or

3/float(4)

This applies for python 2.x and not for python 3

BUT

This doesn't make sense at all, and unless I'm missing something I think it is "wrong"!

This would make much more sense:

cars = 100
space_in_a_car = 4 #not float
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 / float(cars_driven)  #added float

Since the amount of space in a car couldn't be 4 and a half seats, and the average, could be 2 and half persons, since it is a number and not actually persons.

like image 123
Trufa Avatar answered Oct 05 '22 23:10

Trufa


In the code as given in your question, there is no good reason for using a floating point value.

like image 38
David Heffernan Avatar answered Oct 05 '22 22:10

David Heffernan