Eseila
Eseila

Reputation: 43

Is there a way to print at which point for loops ended?

I have a little thing here which checks if a number is prime or not:

num = int(input("Choose a number: "))

for i in range(2, num):
    if num % i == 0:
        print("Not prime")
        break
else: print("prime")

What I would like to add is the output on which number the foor loop ended, so which number is the smallest divisor of num Example: for num = 8 it would be 2. For num = 21 it would be 3

How can I implement that?

Upvotes: 1

Views: 59

Answers (1)

Kalana
Kalana

Reputation: 6173

you can try this:

for i in range(2, num):
    if num % i == 0:
        print(num, " is Not prime")
        print("Smallest devisor of num is = ", i)
        break
    else: 
        print("prime")

Upvotes: 1

Related Questions