-3

I am a newbie of python here. I have been learning with "Learning Python the hard way" but I'm facing a problem on exercise 4, extra credit, question 1.

cars = 100
space_in_a_car = 4.0
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 used 4.0 for space_in_a_car, but is that necessary? Why 4.0 is used instead of 4 in this case?

I hope guys can help me to explain why.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Nothing
  • 147
  • 2
  • 4
    Maybe you should tell us what your problem is. – Hans Then Sep 20 '12 at 11:48
  • Please post the text of the question, and show us what you've tried so far. – Kevin Sep 20 '12 at 11:48
  • 3
    I don't believe it's good to just downvote the first question of a new user, try to be a little more understanding. There have been suggestions how the question needs to be improved, so just give it some time. – phant0m Sep 20 '12 at 11:49
  • You should always post the question, as the page you are linking to might become unavailable at a later date. People finding this question later will thus not understand the point of it. And if that isn't bad enough, asking first for help and then asking for helpers to look at another page to see your issue in context, is not good practice. – InanisAtheos Sep 20 '12 at 12:01
  • 1
    ok , sorry for my posting . I will try to correct myself . – Nothing Sep 20 '12 at 12:03

2 Answers2

2

What the 4.0 does is type the variable to a float instead of an int. The result of this is that in the output carpool_capacity is typed float too and reads 120.0 instead of 120.

Edit:

In your example there is no reason to use float precision instead of int precision for space_in_car. The only place where it could be usefill is to calculate average_passengers_per_car = passengers / cars_driven. Because after the division you would lose everything behind the comma as it is immediately cast to an int.

Minion91
  • 1,911
  • 12
  • 19
  • sorry minion the question asked is incorrect . I edited it already . – Nothing Sep 20 '12 at 12:04
  • In python the type is a `float`, not a double. I think the exercise is getting at the difference between float and integer division, which might throw off the calculations if you're not aware of it. – Marius Sep 20 '12 at 12:06
2

Summary of what I think the question is asking you to discover

In Python 2.x, the / operator is used for division. However, its behaviour when given two integers such as 5 and 2 is somewhat surprising as most people would expect 2.5 - the result is instead 2. By using 5.0 / 2 or 5 / 2.0 (or even float(5) / 2) the interpreter will return the expected answer 2.5. So put simply - if the operation is purely on integers, then the result will be an integer, with possible loss of precision.

This changed in Python 3.x whereby the default behaviour is to not lose precision. eg, 5 / 2 will indeed give you 2.5... and you have to explicitly declare you don't mind loss of precision (eg, the behaviour in Python 2.x) by using the // operator, which is integer division only - eg, in Python 3.x, 5 // 2 is the same as 5 / 2 in Python 2.x. The // operator is also in Python 2.x. if you wish to be explicit about it.

And just to really make it as clear as mud, you can use from __future__ import division at the top of a module in Python 2.6 (might be 2.5 - can't remember) to make the / operator behave as per Python 3.x.

As a note - if you read down to the bottom of the pgae, the author has this as a FAQ.

Why did you use 4.0 for space?

It is mostly so you can then find out what a floating point number is and ask this questions. See the extra credit

Jon Clements
  • 138,671
  • 33
  • 247
  • 280