Ask coding questions

← Back to all posts
How can I make something happen if [variable] is divisible by [number]?
SeanQuangNguyen (0)

How can I make something happen if a variable is divisible by a certain number? For example, if my coins are divisible by ten, I will get a bomb that blows up the entire planet. In my multiplication repl, a "hard question" will appear for every certain amount of questions. I think I need some help for knowing how to implement this. I'm pretty sure it's something like:
"Questions = AnswersRight + AnswersWrong"
and then
"if (Questions % 10) == 0:"
[whatever else I'm going to add]

Wumi4 (531)

This is simple, just use normal if and modulo operator (%). The modulo operator basically divides your two numbers, then return the remainder.

if <variable> % <number> == 0:
   # Something here...

Since you are using an if chain, use else if (or just elif) to continue the chain.

if <condition>:
   # Something...
elif <your_condition_here>:
   # Something else...
   # Something else else...

Also, I want to point out that in the while loop, you imported random twice, which is unnecessary, just import it once on the first line of the file.

import random

# The rest of your code here...