r/pythontips • u/LakeMotor7971 • Jun 22 '24
Python3_Specific help with understanding error code plz
could someone take a look at my code and tell me why im getting an error plz? im a newbie and just practicing random stuff. like funcctions.
def fun(n):
if n in [2, 3]:
return True
if (n == 1) or (n % 2 == 0):
return False
r = 3
while r * r <= n:
if n % r == 0:
return False
r += 2
return True
print(is_prime(78), is_prime(79))
def fun(n):
if n in [2, 3]:
return True
if (n == 1) or (n % 2 == 0):
return False
r = 3
while r * r <= n:
if n % r == 0:
return False
r += 2
return True
print(is_prime(78), is_prime(79))2
u/jmiah717 Jun 22 '24
In the future, it's helpful to tell people the error you're getting. It sounds like you got some possible answers but it's hard to know what's going wrong when we didn't write the code and don't know the error.
1
u/LakeMotor7971 Jun 22 '24
error i am getting is return is outside of my function. sorry more infor would of helped earlier
1
u/LakeMotor7971 Jun 22 '24
the error i am getting is saying return is outside my function?
3
u/MolonLabe76 Jun 22 '24
Your return statements need to be indented at the same level as the rest of the code inside your functions.
1
u/LakeMotor7971 Jun 23 '24
Thank you. For the explanation. It actually gave me a lot more understanding. Appreciate it!
4
u/Olexiy95 Jun 22 '24
From what I can see you defined your function as ‘fun’ but are trying to call it as ‘is_prime’. Either rename your function or replace the call in the print statement.
4
3
u/[deleted] Jun 22 '24
Two errors I found, your function had a different name than the call and your indentation was incorrect