1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
6
Rep Power
0

#### 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
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
8
Rep Power
0
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
3. 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 look-ahead 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
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
6
Rep Power
0
Thanks for the replies. Thanks for posting the correct programming understanind of the order of functions. It makes sense now.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
6
Rep Power
0
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."
6. 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```