2
The exercise speaks the following :
"Write a function that takes a positive integer m and returns 1 if m is prime, 0 otherwise."
My attempt :
Edit: Then , performed the modifications the program is still not returning the values of 1 or 0 ( if the number is odd or even).
m = int(input("Digite um numero inteiro:"))
def recebe_m(m):
n_divisores = 0
i = 1
while(i <= m):
if(m%i==0):
n_divisores = n_divisores + 1
i = i+1
if(n_divisores==2):
return(1)
else:
return(0)
print(recebe_m(m))
BS : I have chosen to leave the i <= m , I hope you have no problem.
I’m getting the following python error :
Unboundlocalerror: local variable 'i' referenced before assignment
Your code is all wrong. Are you trying to run the code exactly like this or just failed to format it by posting it here?
– Woss
I guess I’m failing to post the code here Anderson, I’m not sure how you do it.
– user158657
And how did you test it? You can post the code on Repl.it? I tested here with the correct indexes and returned 0 and 1 as expected.
– Woss
I tested it and it didn’t work. What did you do? Copied the conium and put in the Repl.it ?
– user158657
Yes, adjusting all incorrect indentations.
– Woss
I did it now. I was copying from my own text , when I copied from yours worked. What was I wrong ? Just identation ? Edit: I think I figured out what I did wrong. i = i +1 is out of if.
– user158657