0
Need help
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): prime_numbers = [] for num in range(a, b + 1): if isPrime(num): prime_numbers.append(num) return prime_numbers f = int(input()) t = int(input()) print(primeGenerator(f, t))
4 Respostas
+ 5
mustafa mustaf ,
make also sure that the for loop in the `primesGenerator(...)` function is like:
...
for num in range(a, b): # <<< upper bound should not be included
...
+ 1
What's it that you need help with?
+ 1
mustafa mustaf ,
Please remove your old tags, and add these: python, generator, help
I recognize that code. It's from:
"Python Developer" course
"Functional Programming" module
"Generators" practice
Most of the code is pre-written by Sololearn, including the first line of the generator function. You only have to provide the indented statements for the body of the generator function.
You modified the print statement and maybe more. You should use Reset Code and rebuild the generator body without changing anything else.
Inside the generator, make a loop that tests the numbers in the range, as you started, but don't build the list inside the generator.
Use yield inside the loop to yield one number at a time if it is prime.
(A generator function must include a yield statement.)
The list will get built in the print statement that calls the generator and receives the yielded primes.
Sololearn's generator explanation is thin, so also do a web search for better explanations.
0
Nothing is wrong your code, the code itself is correct, try using a different python editor (if you have a pc, run the code in vscode or there are python editors you can download on the play store), sololearn's editor is sub-optimal. Hope that helps