Harvey Harris
Harvey Harris

Reputation: 13

Why am I not getting any output from my code?

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

Answers (3)

uma_8331
uma_8331

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

Santiago
Santiago

Reputation: 78

Give this version a shot.

Main issues were:

  1. The prime == False statement under the first if loop is set up as a boolean expression, not a variable assignment.
  2. Set the 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 designation
prime = 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

Pablo C
Pablo C

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

Related Questions