Python Forum
Understanding compound operators -=
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding compound operators -=
#1
Hello, I am trying to self learn python from a book called the coders apprentice. I really like the book and I am learning from it. I encountered an exercise that asks you to determine how many dollars, quarters, dimes, nickels and pennies of change you will have in a specific amount for example 1156 cents. The code for the program is here

CENTS_IN_DOLLAR = 100
CENTS_IN_QUARTER = 25
CENTS_IN_DIME = 10
CENTS_IN_NICKEL = 5

amount = 1156
cents = amount

dollars = int( cents / CENTS_IN_DOLLAR )
cents -= dollars * CENTS_IN_DOLLAR
quarters = int( cents / CENTS_IN_QUARTER )
cents -= quarters * CENTS_IN_QUARTER
dimes = int( cents / CENTS_IN_DIME )
cents -= dimes * CENTS_IN_DIME
nickels = int( cents / CENTS_IN_NICKEL )
cents -= nickels * CENTS_IN_NICKEL
cents = int( cents )

print( amount / CENTS_IN_DOLLAR, "consists of:" )
print( "Dollars:", dollars )
print( "Quarters:", quarters )
print( "Dimes:", dimes )
print( "Nickels:", nickels )
print( "Pennies:", cents )
I understand when something is x = x+5
but with this program I dont see how it works and tried to understand how the compound operators work and I am stuck. Can someone please explain to me how the cents obtains its new value from the calculation on the right?

Thank you very much
Reply
#2
Please read the forum rules and use CODE TAGS around your code in the future.

The operators are shorthand:
x += 5 is shorthand for x = x + 5

cents -= nickels * CENTS_IN_NICKEL is shortand for cents = cents - nickels * CENTS_IN_NICKEL

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#3
Thank you I will wrap the code next time, thank you very much that clarifies it.
Reply
#4
Just something to think about.
The book looks OK, except for one thing. The author likes to use single letter variable names.
Be aware that this is considered bad practice. Meaningful names identify what the variable is used for. Single letters can never do this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of if - and operators Pedro_Castillo 1 489 Oct-24-2023, 08:33 AM
Last Post: deanhystad
  Compound if else statement Led_Zeppelin 8 1,680 Sep-09-2022, 07:27 PM
Last Post: deanhystad
  Mixing Boolean and comparison operators Mark17 3 1,405 Jul-11-2022, 02:20 AM
Last Post: perfringo
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 2,318 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  Class and Operators in Python rsherry8 1 1,990 May-27-2020, 07:09 PM
Last Post: buran
  Trying Comparison Operators PythonGainz 3 2,704 Mar-28-2020, 10:46 AM
Last Post: PythonGainz
  Mathematical Operators in String AgileAVS 1 2,382 Mar-04-2020, 04:14 PM
Last Post: Gribouillis
  A doubt with 'in' and 'not in' operators with strings newbieAuggie2019 7 3,586 Oct-23-2019, 03:11 PM
Last Post: perfringo
  understanding exponential and bitwise operators srm 1 2,044 Jun-15-2019, 11:14 AM
Last Post: ThomasL
  please help with this question about using operators to multiply a string? GilesTwigg 3 4,381 Feb-27-2019, 04:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020