This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
You haven't posted any discussions yet.
Ok...I'll start. Since I just completed exercise 4, I will post that.
When I did this, I was not sure how to treat spaces, and how it would look in the output.
I did not do the Extra Credit yet.
This was my code:
# assigns 100 to the variable "cars." In other words, there are 100 cars
cars=100
# assigns 4.0 to the variable "space_in_a_car." This is the number of occupants allowed in the car
space_in_a_car=4
#assigns 30 to the variable drivers. This is the numebr of pssible drivers
drivers=30
#assigns 90 to the variable passengers. This is the number of passengers that need to be carpooled.
passengers=90
#the number of cars that are not driven is the difference between the number of cars and the number of drivers
cars_not_driven=cars-drivers
#the number of drivers is the number of cars driven
cars_driven=drivers
#the maximum number of people possible in the carpool is the number of cars driven, or the number of drivers* the number of passengers in the car
carpool_capacity=cars_driven*space_in_a_car
#average number of people in a cars
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."
Yeah, I am really digging using preset variables. I haven't looked a head yet to see when/if we get the dynamic variables.
Perfect.. Thats the same stuff I got. Now from my understanding a floating number is when you use a decimal point and they outcome of (function or mathematical equation) will come out with an answer equal to the number of spots after the decimal??? Thats what I gathered atleast please correct or lmk what anyone else got from the descriptions.
That is what I believe to be the case regarding floating point decimals. Defining a variable as 1.00 would produce results with two decimal places.
:)
Try it! What happens when you print 6.000? What happens if you try 2.00*3?
So I misread 2.00*3 as 2.00/3 at first, and was returned the result of 0.66666666666666663. Never before have I seen 2/3 end in a 3. Why is this? The only reason I can think of (and I haven't thought about it enough to make any real sense of it) is that .66666.....6666 rounds to .66666.....6667 and the difference between 10 and 7 is three. And I don't think that makes very much sense.
Hint: computers are base 2.
Mine did not end in a 3
>>> 2.00/3
0.6666666666666666
hmm...
same here, mine did not end with a 3, mind to mention version of python used? that could be the case :).
OK...This is NOT reinforcing my confidence in Python's calculations :)
>>> 2.00*3.0
6.0
>>> 2.00*3
6.0
>>> 2.00000*3.000
6.0
>>> 2.01*3
6.029999999999999
>>>
So then I read this: http://docs.python.org/tutorial/floatingpoint.html
I'd like to vouch for that doc too, it helped me (fatally bad at math) understand floating points better.
I'll try paraphrasing here, and maybe others can correct me: when I was a kid we'd learn to work with fractions like 1/3 and 1/4, which are very tidy and definitive-looking. But if you actually plug 1 divided by 3 into a calculator, you get a very sprawling mess like 0.33333333333 which goes on for as much space your calculator window has, maybe rounded up or down at the end to appear less depressing. That rounding is artificial, technically those threes go on forever, and your calculator is forced to cut it off at some point. But the more threes it displays, the more accurately it's portraying 1/3. Since it can't display infinite threes or proper fractions, the calculator's answer is just an approximation - maybe a very good one for most people's purposes, but an approximation nonetheless. That's why 1/3 x 3 = 1, but 0.333333 x 3 = 0.999999...
Likewise, the format my computer uses for numbers is sprawling, like a calculator. Python (somewhat misleadingly) displays clean-looking truncated numbers even though it's actually working with the sprawling versions "behind the scenes". So if you tell Python to round the number "2.675" to 2 decimals, we'd expect it to say "2.68" (since 5 gets rounded up, obviously). But Python will actually tell you that it's "2.67". Why? Because behind the scenes, the actual number Python's using is "2.674999999...", and 4 gets rounded down.
Anyway, if you enter a math problem like 1 / 3 without using the floating point (i.e., a ".0" after the whole number), it'll give you back an answer artificially rounded to the nearest whole number (1 / 3 = 0). If you type in the problem using floating points (1.0 / 3.0) it'll give you 0.333333333333. So if you want to display a more accurate answer, use floating points...otherwise Python will tell you that 1/3 + 1/3 + 1/3 = 0.
This is what I got also.
I have a question about the extra credit in Exercise 4. Question 1 asks why we used 4.0 instead of 4. I can't seem to figure out a good reason for this as both ways seem to work. The only thing that comes to mind is that space_in_car might not be a whole number. That's as logical as I can get to.
Edit: P.S. -- I got the same answers as everybody above.
hi, I think you add a ".0" behind to force it to be a floating point calculation? so that you could get the decimal values
for example, 2/3 in IDLE could give u 0 (Python 2.7, python 3 gave me 0.66666666)
2.0/3 is 0.666666666
it seem that when both are integers, and the result has decimal values, the decimal values are taken off, leaving only the whole number.
Oh...i see what you are saying...thank you for the reply.
I'm still not sure I understand why the floating point number, 4.0, is necessary. In Exercise 4 the variable 'space_in_a_car' is an integer and is only ever used in the 'carpool_capacity' equation. The other variable in this equation ('cars_driven') is an integer too (assuming no. of drivers cannot be a decimal value, e.g. 30.5 drivers) therefore the product of these two variables ('cars_driven' * 'space_in_a_car') can only be an integer also.
In ZM L's example above, I understand the necessity for floating point numbers because the result of a division may be a decimal value but our equation is a multiplication which does not produce decimal values. Am I missing something?
Output:
>>>
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.
>>>
#6 Extra credit:
code
x = 4
y = 10
a = 8
print (x * y) / a
output:
>>>
5
>>>
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."
====
EC 0: passenger is not the same as passengers
EC 1: makes carpool_capacity be calculated as a float. (But note that line 8 doesn't have any floats, is an int, good for a number of people.)
Did EC 0-6
$ 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.
#How many cars?
cars = 100
#This number is a float because the space in a car varies - It could include minivans, for instance. This number is an average, and could be a fraction.
space_in_a_car = 4.0
#simple integer
drivers = 30
#Simple integer
passengers = 90
#Simple enough.
cars_not_driven = cars - drivers
#A car cannot operate without a driver, so the driver is therefore the dependent number in this equation. A potential flaw: Should the number of drivers outstrip the number of cars, the program would need a re-write.
cars_driven = drivers
#Expressed as a floating point integer, because of space_in_a_car
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."
In the extra credit for exercise 4, what is the NameError that Python alerted Shaw to in line 8? Is it the underscore that was added to car_pool_capacity (sted carpool_capacity), or is that a genuine typo in Shaw's manuscript (a la the "passenger" sted "passengers" later in the same line)?
After adding comments, here's my program. I put spaces after each line to make reading it here easier.
On my first run I'd misspelled passengers on line 23. Spotted it right away, corrected it and then it ran fine.
1# Define number of cars
2cars = 100
3# Define number of spaces per car
4space_in_a_car = 4.0
5# Define number of drivers per car
6drivers = 30
7# Define number of total passengers
8passengers = 90
9# Setup calculation for cars not driven based on number of cars and drivers
10cars_not_driven = cars - drivers
11# Setup an equal relationship of cars to drivers
12cars_driven = drivers
13# Setup a relationship between carpool capacity and the number of spaces available per car
14carpool_capacity = cars_driven * space_in_a_car
15# Setup a relationship for average passengers based on total passengers and cars driven
16average_passengers_per_car = passengers / cars_driven
17
18
19print "There are", cars, "cars available."
20print "There are", drivers, "drivers available."
21print "There will be", carpool_capacity, "people today."
22print "We have", passengers, "to carpool today."
23print "We need to put about", average_passengers_per_car, "in each car."