Introduction To Python - Learn Python the hard way v2.0
Title: Exercise 4
prasad@pjoshi:~/programs/python$ ./ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.
#### EXTRA CREDITS ####
Traceback (most recent call last):
File "ex4.py", line 8, in
average_passengers_per_car = car_pool_capacity / passenger
NameError: name 'car_pool_capacity' is not defined
There seems to a typo on line 8 of file ex4.py. Line 8 uses variable 'car_pool_capacity', however no such variable is pre-defined, hence reading from undefined variable prints stack trace. This line should have been changed to
average_passengers_per_car = carpool_capacity / passenger
#### EXTRA CREDITS 1 ####
1. Explain why the 4.0 is used instead of just 4.
Using 4.0 instead of 4 ensures floating point arithmetic is used during calculations. However, as we can see from the output of the program, non of the statement produces fractional numbers. Therefore, even if we replace line 4 to
space_in_a_car = 4
output would be same. For example,
prasad@pjoshi:~/programs/python$ cat ./ex4.py
#!/usr/bin/python
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."
prasad@pjoshi:~/programs/python$ ./ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120 people today.
We have 90 to carpool today.
We need to put about 3 in each car.
#### EXTRA CREDITS 3 ####
prasad@pjoshi:~/programs/python$ cat ex4.py
#!/usr/bin/python
# number of cars
cars = 100
# space in each car
space_in_a_car = 4
# number of drivers
drivers = 30
# number of passengers
passengers = 90
# excess cars
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
# uniform distribution
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."
#### EXTRA CREDITS 6 ####
prasad@pjoshi:~/programs/python$ python
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> kb = 1024
>>> mb = kb * kb
>>> gb = mb * kb
>>> print "1KB", kb
1KB 1024
>>> print "1KB=", kb
1KB= 1024
>>> print "2KB=", kb * 2
2KB= 2048
>>> print "2MB=", mb * 2
2MB= 2097152
>>> print "2GB=", gb * 2
2GB= 2147483648
>>> mb * kb == gb
True
