Trying to write a function 'prime_check(number)' to test if an argument is a prime number
The number_of_factors for a given number should only be 1 (the given number itself) and '1', which is already excluded in the while statement
Having trouble adding a variable to the list "set_of_factors" Tried to debug in Thonny, which says that set_of_factors = none
def prime_checker(number):
set_of_factors=[]
number_of_factors=len(set_of_factors)
f=2
while number > 1 and f<=number:
if number%f == 0:
set_of_factors.insert(0,1)
f+=1
else:
f+=1
if number_of_factors==1:
print("It is a prime number")
else:
print("It is not a prime number")
n = int(input("Check this number: "))
prime_checker(number=n)
Prime numbers have two factors, one and itself. Ex. 5's factors would be (1, 5)
Also istead if ".insert" use ".append"
def prime_checker(number):
set_of_factors=[]
number_of_factors=len(set_of_factors)
f=2
while number > 1 and f<=number:
if number%f == 0:
set_of_factors.insert(0)
f+=1
else:
f+=1
if number_of_factors==2:
print("It is a prime number")
else:
print("It is not a prime number")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.