该代码是否正确以检查数字是否为质数?

时间:2019-12-29 07:21:46

标签: python

以下是您尝试发布的格式的代码:

user_number = int(input("Enter a number: "))
a = list(range(2, user_number))
def prime(x):
    for e in a:
        if user_number % e == 0:
            print(str(user_number) + " is not a prime number.")
            break
        else:
            print(str(user_number) + " is a prime number.")
            break
prime(user_number)

2 个答案:

答案 0 :(得分:0)

更正您的代码,

Development

答案 1 :(得分:0)

这是更有效的方法。

num = int(input("Enter a number: "))
if num > 1:

    for i in range(2,num):
        if (num % i) == 0:
            print(num,"is not a prime number")
        break
    else:
        print(num,"is a prime number")
else:
    print(num,"is not a prime number")