Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
num = int(input("enter a number")

i=2
k=0

while num>i:
    if num%i==0:
       k=1
       print("Number is not a prime number")
       i+=1
       break
if k==0:
   print("number is a prime number")


What I have tried:

i want to know why we use k in this code.
Posted
Updated 30-Jun-20 22:39pm
v2

Indentation is important in Python, so it is really critical that you format your code when you post it here - flat to to the left won;t work in Python! I added the tags for you - use the Improve Question widget to see what I did.

It's being used as boolean value: 0 means "I didn't find it, so it's prime", and 1 means "I found a number that divides into it, so it isn't prime".
It starts as "prime" as the default, and gets changed if it's proven to be non-prime.
 
Share this answer
 
First of all, there is a bug, try with input 5 or 7.
Quote:
i want to know why we use k in this code.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Even if properly indented (thanks to Griff), your code is broken. Try instead
Python
num = int(input("enter a number "))
i = 2
prime = True

while num > i:
  if num % i == 0:
    prime = False
    break
  i += 1

if prime == True:
  print("number is a prime number")
else:
  print("Number is not a prime number")
Please note, this is a particular inefficient way to establish if a number is prime.


[Update]
As 0x01AA gently :-) suggests, you may also write
Python
if prime:
  print("number is a prime number")
else:
  print("Number is not a prime number")

[/Update]
 
Share this answer
 
v2
Comments
[no name] 30-Jun-20 8:05am    
if prime == True: hurts my eyes :) Otherwise a 5
CPallini 30-Jun-20 8:09am    
You have my simpathy :-D

Thank you!
p.i.n.k.u 1-Jul-20 5:12am    
is it ok to use break in this code instead of boolean value?
num = int(input("Enter the number"))

i = 2

while i < num:

if num % i == 0:
print("Entered number is not a prime number")
i += 1
break
else:
print("entered number is prime number")
break
CPallini 1-Jul-20 5:14am    
That way it is NOT OK (it could be OK if correctly implemented)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900