Sep-12-2019, 08:55 AM
(This post was last modified: Sep-12-2019, 08:56 AM by YoungGrassHopper.)
Hey guys.
So what I have today is another nested loop issue.
Program needs to ask a user for a number and save as variable (num)
I then need to determine if the number is bigger than 1, and if so:
determine if the number given by the user is a prime number or not.
Chances are fair that my approach is wrong but here is what I have come up with:
Trying to use a while loop as outer loop to start the inner for loop calculation
ONLY IF the number given by the user is greater than one.
at first I only had the for loop but I get a false positive if the user enters the number 1.
It seems to work apart from the fact that its now an infinite loop, and moving the break command back seems
to kill the calculation.
Any advice would be very much appreciated.
Here is what I have:
So what I have today is another nested loop issue.
Program needs to ask a user for a number and save as variable (num)
I then need to determine if the number is bigger than 1, and if so:
determine if the number given by the user is a prime number or not.
Chances are fair that my approach is wrong but here is what I have come up with:
Trying to use a while loop as outer loop to start the inner for loop calculation
ONLY IF the number given by the user is greater than one.
at first I only had the for loop but I get a false positive if the user enters the number 1.
It seems to work apart from the fact that its now an infinite loop, and moving the break command back seems
to kill the calculation.
Any advice would be very much appreciated.
Here is what I have:
num = int(input("Enter a number here: ")) while num >= 2: for i in range(2, num): if num % i == 0: print(num," is not a prime number.") break else: print(num," is a prime number.")