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", aver开发者_Go百科age_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?
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.
In the code as given in your question, there is no good reason for using a floating point value.
In general, integer div is not the same than floating point div
integer div:
myInt = 4 / 5
myInt == 0
float div:
myFloat = 4.0 / 5.0
myFloat = 0.8
by making space_in_car a float, then the results of the operations are calculated as floats (no rounding)
But in this sample space_in_car is only used for multiplication .For multiplication the same goes, but the value does not change (120 is the same than 120.0). My guess is that it was intended to be used in a division to show the above mentioned properties but there was a lapsus calami.
I think there is an error in the wording: since space_in_a_car appears only in a multiplication, there is no possibility to observe a problem when changing its value.
A problem could be observed if the value of drivers was not a divisor of the value of passengers
See what happens when changing the value of drivers :
form = ('space_in_a_car : %s\n'
'drivers : %s\n'
'passengers : %s\n'
'cars_driven : %s\n'
'average_passengers_per_car : %s\n\n')
for nbdrivers in (30, 30.0, 47, 47.0):
cars = 100
space_in_a_car = 4
drivers, passengers = nbdrivers, 90
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
print form % (space_in_a_car,
drivers,
passengers,
cars_driven,
average_passengers_per_car)
result
space_in_a_car : 4
drivers : 30
passengers : 90
cars_driven : 30
average_passengers_per_car : 3
space_in_a_car : 4
drivers : 30.0
passengers : 90
cars_driven : 30.0
average_passengers_per_car : 3.0
space_in_a_car : 4
drivers : 47
passengers : 90
cars_driven : 47
average_passengers_per_car : 1
space_in_a_car : 4
drivers : 47.0
passengers : 90
cars_driven : 47.0
average_passengers_per_car : 1.91489361702
Then , it is drivers that must be a float, not space_in_a_car
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
# 90 % 4 = 2(man), 90 / 4.0 = 22.5(car)
print "Need %d cars." % int(round(90 / 4.0))
# He wants you to think more about it.
精彩评论