如何检查变量中是否满足每个 X 数?
How to check if every X number is met within a variable?
在Python中,我在循环中累积一个变量,直到满足某个条件。但是,我如何检查 "var1" 中每 100 个累积数字是否满足。我可以手动完成(“如果 var1 == 100、200、300 等”),但这并不好。
var1 = 0
while var1 != 10000:
var1 += 1
您可以使用模运算符 %
:
var1 = 0
while var1 != 10000:
if var1%10 == 0:
do something
var1 += 1
Modulus operator, it is used for remainder division on integers, typically, but in Python can be used for floating point numbers. The % (modulo) operator yields the remainder from the division of the first argument by the second
在Python中,我在循环中累积一个变量,直到满足某个条件。但是,我如何检查 "var1" 中每 100 个累积数字是否满足。我可以手动完成(“如果 var1 == 100、200、300 等”),但这并不好。
var1 = 0
while var1 != 10000:
var1 += 1
您可以使用模运算符 %
:
var1 = 0
while var1 != 10000:
if var1%10 == 0:
do something
var1 += 1
Modulus operator, it is used for remainder division on integers, typically, but in Python can be used for floating point numbers. The % (modulo) operator yields the remainder from the division of the first argument by the second