I want to make a program that can check whether the entered number is a prime number in Jupyter Notebook. This is the code:
def input_number():
number = input()
if number.isnumeric():
the_number = int(number)
def check_prime():
divisor = 1
divisor += 1
if the_number > 1:
if divisor in range(2, the_number):
if the_number % divisor != 0:
print(the_number, "is a prime number")
else:
print(the_number,"not a prime number")
print(the_number, "divide", number//divisor, "is", divisor)
else:
print(the_number, "not a prime number")
else:
But when I enter a number the process will not continue to def check_prime
and it just freezes. If I enter anything other than a number then I get
**UnboundLocalError: cannot access local variable 'check_prime' where it is not associated with a value**
You defined that function under input_number()
You can only use check_prime()
under that function.
define the check_prime()
outside of input_number()
.
def input_number(): #input number func
number = input() #take the number
return int(number) if number.isnumeric() else print('Input only INT.') #return the number swapped to int if its numeric.
def check_prime(the_number): #prime function - num is a parameter to use in function
# you defined divisor than added 1 , but its same with defining it as 2.
divisor = 2 # you also dont need to define divisor
if the_number > 1:
if divisor in range(2, the_number):
if the_number % divisor != 0:
print(the_number, "is a prime number")
else:
print(the_number, "not a prime number")
print(the_number, "divide", the_number // divisor, "is", divisor)
else:
print(the_number,'not a prime number.')
while calling, use
check_prime(input_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.