0

Fucntion programming

I have an issue with a script from the pro code coach Located at -> intermediate python -> functional programming -> generators The script question is as follows: The given code defines a function isPrime(x), which returns True if x is prime. You need to create a generator function primeGenerator(), that will take two numbers as arguments, and use the isPrime() function to output the prime numbers in the given range (between the two arguments). Sample Input: 10 20 Sample Output: [11, 13, 17, 19] My solution so far: def isPrime(x): if x < 2: return False elif x == 2: return True for n in range(2, x): if x % n ==0: return False return True def primeGenerator(a, b): #your code goes here values = [a,b] yield isPrime(values) f = int(input()) t = int(input()) print(list(primeGenerator(f, t))) I do however get these errors: Traceback (most recent call last): File "/usercode/file0.py", line 19, in <module> print(list(primeGenerator(f, t))) File "/usercode/file0.py", line 14, in primeGenerator yield isPrime(values) File "/usercode/file0.py", line 2, in isPrime if x < 2: TypeError: '<' not supported between instances of 'list' and 'int' What am I doing wrong with the generators

24th Feb 2021, 4:41 PM
Kristian Sørensen
Kristian Sørensen - avatar
5 Answers
+ 1
You are passing a list to isPrime function, Something like this would help, def isPrime(x): if x < 2: return False elif x == 2: return x for n in range(2, x): if x % n ==0: return False return x def primeGenerator(a, b): #your code goes here for i in range(a,b): yield isPrime(i) f = int(input()) t = int(input()) print(list(filter(lambda x:x,(primeGenerator(f, t))))) filter function removes all false from the generator object but there must be a way to not return False (not sure how to do it). Just saying ,that there is no need to check for 2 to x numbers ,2 to math.sqrt(x) is enough . 𝙀𝙙𝙞𝙩: Just checked to see if math.sqrt works but i think it will be better to do without it since i can't exactly tell how to use it .
24th Feb 2021, 5:17 PM
Abhay
Abhay - avatar
0
posting pro code coach solutions is against sololearn rules ;P
24th Feb 2021, 5:09 PM
visph
visph - avatar
0
visph oh, how is some one supposed to get help, if one cant get it right? 😀
24th Feb 2021, 5:43 PM
Kristian Sørensen
Kristian Sørensen - avatar
0
don't ask to me (or us), but rather to sololearn crew ^^
24th Feb 2021, 5:46 PM
visph
visph - avatar
0
@abhay def isPrime(x): if x < 2: return False elif x == 2: return True for n in range(2, x): if x % n ==0: return False return True def primeGenerator(a, b): #your code goes here #create a liste to add each true prime number for i in range(a, b+1): if isPrime(i): yield i #values = [a,b] #yield isPrime(values[0]), isPrime(values[1]) f = int(input()) t = int(input()) print(list(primeGenerator(f, t))) I got to this code which is working in 4 out of 5 examples, but since I cant watch the 5th input its hard to know why its going wrong there
24th Feb 2021, 8:09 PM
Kristian Sørensen
Kristian Sørensen - avatar