Unit 2 Task 31: Is this number prime?

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Unit 2 Task 31 - Is this number prime?

← Unit 2 tasks · Solution


Ask for an integer n (assume n ≥ 2). A prime has no divisors other than 1 and itself.

Use a loop to test if any integer from 2 to n − 1 divides n evenly (% == 0). If none do, it is prime.

Print prime or not prime.

Skills: for / for with else in Python, or boolean flag.

Stretch: treat n == 1 as not prime if you want to allow any input.