【问题标题】:code that check if number is prime number [duplicate]检查数字是否为素数的代码[重复]
【发布时间】:2015-09-30 01:29:25
【问题描述】:

我有检查数字是质数还是音符的代码

当我运行它给我一个错误

我的代码:

a = int (input("your numbre :"))
b = range(1,a+1)
if a%b == 0:
    for i in b :
        if b == a or b == 1:
            print("the numbre you input is prim number")
else:
    print("the nmubre you input is note prem numbre")

产生的错误:

Traceback (most recent call last):
  File "C:\Users\admin\Desktop\chap8ex2.py", line 3, in <module>
    if a%b == 0:
TypeError: unsupported operand type(s) for %: 'int' and 'range'

【问题讨论】:

标签: python primes


【解决方案1】:

范围函数在其参数之间创建一个数字数组。 例如:

>>> range(1, 4)
=> [1, 2, 3]

在您的代码中,您说的是a%b,它正在查找用户输入的数字和范围对象之间的余数。将范围对象视为 python 列表。所以你正在做的是 5 % [1, 2, 3] 这没有意义。检查这个单独的线程以了解如何在 python 中实现质数检查器。 Python Prime number checker

【讨论】:

    【解决方案2】:
    a = int (input("your numbre :"))
    b = range(1,a+1)
    for i in b :
        if b == a or b == 1:
            if a%b!=0:
                print("the number you input is prim number")
    else:
        print("the number you input is not a prime number")
    

    【讨论】:

      猜你喜欢
      • 2020-09-20
      • 2020-07-19
      • 1970-01-01
      • 2021-04-20
      • 2013-03-22
      • 1970-01-01
      • 2015-03-17
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多