Some help in function exercise (python)

Asked

Viewed 372 times

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?

  • I guess I’m failing to post the code here Anderson, I’m not sure how you do it.

  • 1

    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.

  • I tested it and it didn’t work. What did you do? Copied the conium and put in the Repl.it ?

  • 1

    Yes, adjusting all incorrect indentations.

  • 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.

Show 1 more comment

3 answers

2


You need to declare the variable i within the function recebe_m. The same problem will occur with the variable n_divisores, declare the same within the function.

m = int(input("Digite um numero inteiro:"))    
def recebe_m(m):
     n_divisores = 0
     i = 1
     while(i <= m/2):        
        if(m%i==0):           
           n_divisores = n_divisores + 1

        i = i+1

    if(n_divisores==1):
        return(1)
    else:
        return(0)
print(recebe_m(m))

Also, as the friend of the other answer said, you are trying to divide by 0 in the first iteration of your while loop, fix it and boot i with 1.

The problem with your code is variable scope. In python if you declare a variable in a more internal scope, and the same variable exists in a global scope, the variable of the most internal scope will be the one used by python to make the calculations.

In your code you try to declare and increment a variable on the same line in a case that the variable i does not exist in the internal scope yet, which causes the error. The same goes for n_divisores.

To better understand:

x = 10
def func():
    x = 5
    print(x)
func() # printa 5
print(x) # printa 10, x com valor 5 morreu.

Edit: As @Anderson said, it’s not necessary to check all the numbers, but only half of them (i <=m/2)

  • I think I understand some of the changes, but the program is still not returning the values 1 or 0. I still don’t understand why. I didn’t understand why (n_divisors==1) too. (that would be why Voce divided m by 2 ?)

  • @user158657 I changed something in the code and giving error to me, also I will take a look here. And yes, it is because I divided m by 2, hence the criterion to be cousin is that it is divisible only by 1 (have only one divisor). In my case was identation error, see working on replit

  • @user158657 if you have been able to solve your problem with the help of any of the answers, mark the one you helped the most as "answered" to signal that your doubt has been resolved.

0

I tried to make the function as simple as possible

You do not need to account for the divisors, if there is any divisor between 1 and half of itself, you can already define that the number is not prime.

The following function runs through all numbers of 2 até m/2, and if you find any splitter returns 0, if it finishes the loop without finding any it returns 1

m = int(input("Digite um numero inteiro:"))

def recebe_m(m):
  for i in range(2, int(m/2)):
    if m%i==0:           
      return(0)

  return(1)

print(recebe_m(m))
  • 2

    "Don’t give the fish, teach the fish"

  • 1

    I put the explanation so he’d understand the code

  • 2

    And the biggest possible divisor of a number is half of it, so there’s no point in doing the range until m-1, may only be up int(m/2).

0

From what I could see, would you have the intention a [int]%0 in the first case? It is not divided by zero haha (a n to be q want to open a black hole rsrs)

You need to declare the variable n_divisors within def

the code stays:

m = int(input("Numero: "))
def recebe_m(m):
    cont=0
    for x in range(1,m+1):        
        if(m%x==0):
            cont+=1    
    if(cont==2):
        return(1)
    else:
        return (0)

print(recebe_m(m))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.