0

until this I manage this code where I asked a specific number. I try to put this code in a loop to print the first hundred prime number. If someone can help me, I'm completely lost. I'm beginner in Python.

# Programme pour définir si un nombre est premier ou non 
nombre = int(input("Entrez le nombre : "))
nombre_div = int(0)
for div in range (1,nombre+1):
    if nombre % div == 0:
        nombre_div = nombre_div +1
print ("le nombre de diviseur est : ", nombre_div)

for div in range (1,nombre+1):
    if nombre_div == 2:
        print(nombre,"is prime number")
        break
    else:
        print(nombre, "is not prime number")
        break
wjandrea
  • 23,210
  • 7
  • 49
  • 68
  • 1
    Try to take the code you have now and turn it into a function that takes an int as an argument and returns a bool indicating if the argument is prime. Then you can run call that function for each number until it returns true 100 times. – Patrick Haugh Mar 24 '22 at 18:02
  • 1
    You're doing a lot of stuff twice -- e.g. you don't need to create `nombre_div` **and** do `for div in range(...)`. Those are both trying to do the same thing, but then you're mixing them up inside the loop. I suggest just looking up a Python prime number generator, there are literally thousands of them since it's a common beginner exercise. – Samwise Mar 24 '22 at 18:03
  • You don't need to call `int()` in `int(0)`. It's already an integer. – Barmar Mar 24 '22 at 18:05
  • 1
    If you `break` in both the `if` and `else`, the loop never repeats. – Barmar Mar 24 '22 at 18:05
  • BTW, welcome to Stack Overflow! Check out the [tour] and [ask]. – wjandrea Mar 24 '22 at 18:09

0 Answers0