[英]How to print numbers from range but exclude a number by a given divisible number
I need to print out the range between 0-50 with the exclusion of numbers divisible by 7.我需要打印出 0-50 之间的范围,不包括可被 7 整除的数字。
for x in range(0,50):#for loop range beginning 0-50
if x % 7 == 0:
print(x)
Just check if the number is divisible by using the modulo operator %
.只需使用模运算符
%
检查数字是否可整除。
The expression x % 7
will return the remainder of x divided by 7. If the remainder is not zero, then x is not divisible by 7, so print the number.表达式
x % 7
将返回 x 除以 7 的余数。如果余数不为零,则 x不能被 7 整除,因此打印该数字。
for x in range(0, 50):
if x % 7 != 0:
print(x)
Note that in Python, range(start, end) is [start, end);注意在Python中,range(start, end)是[start, end]; ie, your range includes every number from 0 to 49.
即,您的范围包括从 0 到 49 的每个数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.