python - 当我设定了从7到1000000的范围(增量为7)时,为什么会得到这些数字

标签 python error-handling range

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)) 

进一步阅读:
  • Modulo operator in Python
  • binary-arithmetic-operations-二进制=对2件事进行操作,%在其中。
  • 关于python - 当我设定了从7到1000000的范围(增量为7)时,为什么会得到这些数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48851970/

    相关文章:

    php - PHP使用数组中的foreach定义变量

    c# - 程序有时会崩溃

    r - “RTextTools” - 修复 Linux Ubuntu 上 create_matrix 的错误

    javascript - 在 contenteditable div 中的选定文本上添加跨度

    python - 使用nginx/flup重新请求后,如何运行后台线程?

    用于简单表达式的 Python 正则表达式分词器

    python - 如何用字典键替换数据框列值?

    python - 如何将 'google.cloud.documentai.v1beta2.types.document.Document' 转换为 JSON?

    c++ - 数据类型范围作为节省内存的措施是否重要?

    ruby - 有人可以解释这个解决方案背后的数学原理吗