Please can anyone help me out with this code?? What's the problem. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Please can anyone help me out with this code?? What's the problem.

def is_prime(number): for i in range(2,number): if number%i==0: return False return True print(is_prime(15)) print(is_prime(3)) def prime_numbers(n): for i in range(1,n): if is_prime(number): return i print("\n",prime_numbers(40))

30th Apr 2021, 11:25 AM
Adibeh Esther
Adibeh Esther - avatar
2 Réponses
+ 1
I guess you were trying to pass i and did number instead.....since number is nowhere defined in the function or program it will show not defined error. def prime_numbers(n): for i in range(1,n): if is_prime(i): # ---replaced code--- print(i)
30th Apr 2021, 12:15 PM
Satish Aoudichya
Satish Aoudichya - avatar
+ 1
If you are checking a range of values (from 1 to <n> in the code), then you should be checking this instead, inside the for-loop, if is_prime( i ): Also you might wanna `print( i )` rather than `return i` when <i> is a prime. By changing `prime_numbers()` to print prime numbers in range, you then only need to call `prime_numbers()`, no need to print it, as `prime_numbers()` function doesn't return anything usable to be printed. Remove your `is_prime()` tests for number 15 and 3 when you're done verifying `is_prime()` function. And you can put Python in the thread tags, which clarifies language context better than a '6', up there ☝
30th Apr 2021, 12:27 PM
Ipang