Homework is an adventure and chance to experiment!
First, you asked why xPrimes(5)
gave you up to 5. You stop your loop when y < x, and y goes up each time. This shows you that you can get lost by using small variable names. You could make your code look like this by just renaming things:
def primes_up_to_number(stop_at) :
testing_number = 2
while testing_number < stop_at :
if isItPrime(testing_number) == True :
testing_number += 1
testing_number += 1
print(primes)
This is confusing to me, as you can't get the output [2, 3, 5]
. When testing_number
is 2, you add one to it, then add one again before you check isItPrime
again, checking 4. I am assuming isItPrime
updates some global array primes
.
I think you want to change the code and meaning from stop_at
to number_of_primes
. If so, you should set a counter number_of_primes_found = 0
at start of the function and add one to it each time you find a prime. You should change the expression in the while
loop to keep looping until that number of primes is found.
Have a great day! Keep coding! Keep notes.