我是一個初學Python的學習Python的人。這是我學習的第一個編程語言,我被困在練習中。爲什麼在這個例子中他使用了浮點數?
練習: 「解釋爲什麼使用4.0而不是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."
老實說,我找不到任何理由,他將在2號線以外的其他作爲一個例子,如果我有一個浮點數它會影響表達式求值的其餘部分使用浮點( cars_driven * space_in_a_car)導致120.0。
我錯過了什麼嗎?
因爲他懶得做'從__future__進口division'或切換到python3 – JBernardo 2011-06-15 16:31:01