I am trying to write a short python script that gives the user a prime number starting with 3 and continues providing the next prime number if the user wishes to see it.
Here is the function I have created. When I run it I cannot move past the number 3 which is where it starts by design. I get the print statement "3 is not a prime number" and then the input() field pops up to which I enter 'Yes' and I expect the variable 'count' to be incremented to 4 and the script to check again if it is prime.
It should go through this process checking every number and returning just the primes, then asking the user if they wish to see another one. Any ideas why I am stuck on and endless loop of "3 is not a prime number"
def prime_number():
game_on = True
while True:
count = 3
for num in range(2,count):
if count % num == 0:
print(f'{count} is not a prime number')
break
elif count % num != 0:
print(f'{count} is a prime number')
break
else:
pass
question = input("Would you like to see another prime number?? Please enter Yes or No: ")
if question == "Yes":
count += 1
else:
break
game_on = False
Read more here: https://stackoverflow.com/questions/66279163/using-python-to-create-a-script-that-gives-the-user-the-next-prime-number-in-seq
Content Attribution
This content was originally published by paddyusa at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.