December 17th, 2012, 10:55 PM

Newbie Question
print "Roosters", 100 25 * 3 % 4
Why does this equal 97
I'm looking at 100  75 (3x25) but I don't get what the % 4 does
December 18th, 2012, 05:39 AM

What you are doing is asking for 3% of 4 which is .12
So your formula you have entered is actually the following
100  25 * .12
The answer to this is 97
What answer are you looking for if it is 25 just remove the %4
Jezza
Originally Posted by PGFracing
print "Roosters", 100 25 * 3 % 4
Why does this equal 97
I'm looking at 100  75 (3x25) but I don't get what the % 4 does
December 18th, 2012, 08:57 AM

Answering questions at the forum is a great way to learn python. I encourage it! Jezza's answer is incorrect, however. Most of this answer if for Jezza's benefit.
Python evaluates algebraic expressions involving parenthesis (()), power **, multiplication and divisions (*, /, %), additions (+, ) with the same precedence rules as algebra has, and from left to right.
The % operator is "modulus" meaning "remainder after dividing the left argument by a whole number multiple of the right argument.
100 25 * 3 % 4
python sees 100  25 with lookahead token * .
Multiplication has higher precedence, so python continues reading input. Etceteras. So just as you would in algebra, python evaluates
25*3 equals 75
75 % 4 is, well, 4 goes into 75 eighteen times with 3 left over. 75%4 equals that remainder, 3.
Finally python can subtract.
100  3 equals 97
[code]
Code tags[/code] are essential for python code and Makefiles!
December 18th, 2012, 02:41 PM

Thanks for the replies. Thanks for posting the correct programming understanind of the order of functions. It makes sense now.
December 18th, 2012, 04:15 PM

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 = car_pool_capacity / passenger
# The reason line 8 doesn't work is because car_pool_capacity is 30*4 and that divided by the passengers is 90 which comes out to be 1.33. My question is why does it come up with an error instead of printing(outputing) 1.33 for the answer on the last line request
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, " passengers in each car."
the correct code is:
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."
December 18th, 2012, 04:30 PM

Capital letters, plurality, underscores. Where you put them matters!
Code:
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = car_pool_capacity / passenger
# The reason line 8 doesn't work is because car_pool_capacity is 30*4 and that divided by the passengers is 90 which comes out to be 1.33. My question is why does it come up with an error instead of printing(outputing) 1.33 for the answer on the last line request
You haven't made a
car_pool_capacity
You did make a
carpool_capacity
"and that divided by the passengers"
No, you did not divide by
passengers
you divided by
passenger
I use emacs, I search for my variable names, and I've almost completely eliminated for myself variable name misspelling.
Last edited by b49P23TIvg; December 18th, 2012 at 04:32 PM.
Reason: red added
[code]
Code tags[/code] are essential for python code and Makefiles!