Baitalon Baitalon - 1 month ago 6
Python Question

How to check if number is prime if no divisibles

I'm new to programming, I need to know if it's possible to print a string like "This is a prime number" if there were no results for i

n = int(input("Digite um número inteiro positivo: "))
for i in range(2,n):
if n % i == 0:
print(i)


For example if I typed 5 Nothing show up

If I typed 8 it would show 2 and 4

How can I add a print(n,"is a prime number") if NOTHING shows up in the program?
I couldn't find any command for that

Answer
n = int(input("Digite um número inteiro positivo: "))
printed = False
for i in range(2,n):
    if n % i == 0:
         print(i)
         printed = True
if not printed:
      print(n,"is a prime number") 

This uses a "flag" variable to show if a value was printed.