Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, multiple of 10 if statement [duplicate]

Tags:

python

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.

like image 556
Matthew Westley Avatar asked Oct 31 '12 02:10

Matthew Westley


People also ask

How do you get multiples of 10 in Python?

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 .

How do you check if a 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.

Can you include multiple conditions in an if statement Python?

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.

How do you check if a number is divisible by 10 in Python?

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.”)


2 Answers

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.

like image 93
Matthew Adams Avatar answered Oct 13 '22 21:10

Matthew Adams


You should try using a modulo in your script.

See :

>>> if not 10 % 10:
...     print "OK"
... 
OK
>>> if not 9 % 10:
...     print "OK"
... 
>>> 
like image 33
Gilles Quenot Avatar answered Oct 13 '22 20:10

Gilles Quenot