0
if count == 10 or count == 20 or count == 30 or count == 40 or count == 50 or count == 60 or count == 70 or count == 80 or count == 90:
            sleep(8)

i want this code to cover every number in the times 10 times table for ever, but i don't want to have to repeat the same code for ever to achieve this. Is there any way to code that?

bong
  • 3
  • 1

6 Answers6

2

Use mod 10, e.g.

if count % 10 == 0:
    sleep(8)

This checks if count is evenly divisible by 10 and sleeps if it is. It doesn't matter how large count becomes.

mhawke
  • 84,695
  • 9
  • 117
  • 138
1

Use the modulo operator %.

if count%10 == 0:
    sleep(8)

You can also write

if not count%10:
    sleep(8)

but I find the first version easier to comprehend.

If you want to restrict the range for count, add another condition like this:

if (0 < count < 100) and count%10 == 0:
    sleep(8)
timgeb
  • 76,762
  • 20
  • 123
  • 145
0

What you are looking for is the modulo operator: if(x % 10 == 0):

this will divide by 10 and return the remainer. if you enter a number dividable by 10, i.e the times ten table, it'll return 0. If you enter 11 for example it would return 1.

Vulpex
  • 1,041
  • 8
  • 19
0

When you want to check a value being in a given list, use the in operator

if count in (10, 20, 30, 40, 50, 60, 70, 80, 90):
    sleep(8)

if count in range(10, 100, 10):
    sleep(8)

In you case math rules can be used, like the modulo operator

# for any multiple of 10, like -150 or 168730
if count%10 == 0:
    sleep(8)

# for multiple of 10, between 0 and 100
if (0 < count < 100) and count%10 == 0:
    sleep(8)
azro
  • 53,056
  • 7
  • 34
  • 70
0

You could use the modulo operator "%" to determine whether the number is dividable or not by 10 in this manner (Modulo returns the reminder of the number it divides)

if count % 10 == 0:
    #do something

It will check if the last digit of the number is different or equal to 0

-1

You can use the % operator. It returns the remainder from division. Using the principle that if x is divisible by y with no remainder, x is a multiple of y, we can find out.

So:

num = 0
while True:
    if num % 10 == 0:
        time.sleep(8)
    else:
        pass
    num += 1
Ismail Hafeez
  • 730
  • 3
  • 10