Possible Duplicate:
How do you check whether a number is divisible by another number (Python)?
so I'm writing up a "stopwatch game" in python for a course I'm doing, and I want it so that when the timer stops on a whole number, the player receives a point.
I figure that a "whole number" will be a multiple of 10, as the format would be something like this a;bc:d, and the timer is running at a tenth of a second.
What I need your help for is how would I go about saying, "Oh hey, if he stops on a multiple of 10, give him a point".
I cannot for the life of me figure out how to put specify a multiple of 10.
Use the modulo % operator to check if a number is a multiple of 10, e.g. if 100 % 10 == 0: . The modulo % operator returns the remainder from the division of the first number by the second. If the remainder is 0 , the number is a multiple of 10 .
When you multiply a number by 10 you will get the number attached with zero as the result. eg: 10 × 2 = 20. Multiples of 10 are even numbers that end with 0. If a number is ending with 0 then it is always divisible by 10.
In Python, we can use logical operators (i.e., and, or) to use multiple conditions in the same if statement. Look at the code below.
num = int (input (“Enter the number whose divisibility needs to be checked:”)) div = int (input (“Enter the number with which divisibility needs to be checked:”)) if num%div == 0: print (“The number is divisible.”) else: print (“The number is not divisible.”)
Use the %
(modulo operation). Eg:
>>> 100%10
0
>>> 101%10
1
Basically, x%y
returns the value of the remainder of x/y
, so if the remainder is 0
, than x
is evenly divisible by y
. Therefore, if x%10
returns something other than a zero, x
is not divisible by 10.
You should try using a modulo
in your script.
See :
>>> if not 10 % 10:
... print "OK"
...
OK
>>> if not 9 % 10:
... print "OK"
...
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With