Division and Modulo

So far, we have shown how standard arithmetic operations like addition, multiplication work...

Before we dive into division, it is important to mention that in programming languages, decimal numbers are written with a dot instead of a comma. So, for example, instead of 1,5, we write 1.5. This is important to remember to avoid unnecessary errors.

Now let's look at division and the remainder after division. When you perform division in Python using the / operator, the result will be what we expect, e.g.:

print(3 / 2) # prints 1.5
print(4 / 2) # prints 2.0
print(1 / 0.5) # prints 2.0

But what if we want to find the integer remainder? For that, we would use the modulo operator - %. For example:

print(3 % 2) # prints 1, because 3/2 = 1, remainder 1
print(4 % 2) # prints 0, because 4/2 = 2, remainder 0
print(1 % 0.5) # prints 0, because 1/0.5 = 2, remainder 0

Instructions

On the second line, create a variable wallet with a value of 100.

On the third line, print how much money you will have left after buying as many chocolates as possible using the command:

print(wallet % chocolatePrice)

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

9/10