Reputation: 13
Here's a text-based Python implementation for an algorithm to calculate if a number is prime using a REPEAT...UNTIL loop. whenever I try to run this it doesn't output anything. Can anybody please tell me what is wrong with this code? FYI I'm a beginner. Thank you in advance!
prime = True
number = int(input("Enter a number : "))
counter = 2
while True:
modulus = number % counter
if modulus == 0:
prime == False
counter = counter + 1
#The loop for until statement
if counter == number-1 or prime == False:
break
if prime == True:
print("Prime : ", number)
else:
print("Not prime : ", number)
Upvotes: 1
Views: 56
Reputation: 97
prime = True
number = int(input("Enter a number : "))
counter = 2
while True:
modulus = number % counter
if modulus == 0:
prime == False
counter = counter + 1
#The loop for until statement
if counter == number-1 or prime == False:
break
if prime == True:
print("Prime : ", number)
else:
print("Not prime : ", number)`
As you can see in line number 10 prime = False not prime == False
Upvotes: 0
Reputation: 78
Give this version a shot.
Main issues were:
prime == False
statement under the first if
loop is set up as a boolean expression, not a variable assignment.while
loop to run while prime == True
. In this way, the loop will break the moment it detects the first case of contradiction between the input number and the prime designationprime = True
number = int(input("Enter a number : "))
counter = 2
while prime == True:
modulus = number % counter
if modulus == 0:
prime = False
counter = counter + 1
if counter == number-1 or prime == False:
break
if prime == True:
print("Prime : ", number)
else:
print("Not prime : ", number)
Upvotes: 0
Reputation: 4761
I'm not sure why it doesn't output anything, but the line prime == False
is wrong, it should be
prime = False
Upvotes: 1