打印所有不能被 2 或 3 整除且介于 1 和 50 之间的整数的 Python 程序
当需要打印所有不能被2或3整除且介于1和50之间的元素时,以“while”循环和“if”条件的形式提及约束。
以下是相同的演示-
示例
print("Integers not divisible by 2 and 3, that lie between 1 and 50 are : ") n = 1 while n <= 51: if n % 2 != 0 and n % 3 != 0: print(n) n = n+1输出结果
Integers not divisible by 2 and 3, that lie between 1 and 50 are : 1 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49
解释
n的值被赋值为1。
while循环一直运行,直到此'n'不大于51,
它检查数字是否可以被2或3整除。
如果不能整除,则在控制台上显示该数字。
每次显示后,它都会增加。