Thread: Newbie Question

    #1
  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. #2
  3. 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
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    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
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. 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.
  8. #5
  9. 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."
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    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 03:32 PM. Reason: red added
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo