sevens = range(7, 1000000, 7)
x = int(input("Please enter a positive number less than one million: "))
if x in sevens:
print("{} is divisible by seven.".format(x))
我的问题是为什么我得到以下输出?当范围以7开头时,为什么将3510作为起始数字?
3510, 83517, 83524, 83531, 83538, 83545, 83552, 83559, 83566, 83573, 83580, 83587, 83594, 83601, 83608, 83615, 83622, 83629, 83636, 83643......
错误是否与计算机内存有关,还是我的IDE(Pycharm Jetbrains)中的问题?
谢谢!
最佳答案
要测试一个数字是否可以被另一个数字完全整除,请使用%
(aka Modulo)操作,该操作为您提供了除法运算的其余部分:
x = int(input("Please enter a positive number less than one million: "))
if x%7 == 0:
print("{} is divisible by seven.".format(x))
print("It fits {} times.".format(x//7))
else:
print("{} is not divisible by seven - theres a rest of {}.".format(x,x%7))
进一步阅读:
关于python - 当我设定了从7到1000000的范围(增量为7)时,为什么会得到这些数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48851970/