Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
The for loop
The while loop
Nested loops
break and continue
17. break with for loops
Summary

Instruction

Good job! break also works fine with for loops:

user_input = input('Please provide a nickname for yourself. Don\'t use digits! ')
is_valid = True
for character in user_input:
  if character.isdigit():
    is_valid = False 
    break

if is_valid == True:
  print('Nickname correct!')
else:
  print('Nickname incorrect!')

The program above asks the user to provide a nickname without digits. It then iterates through the nickname. If any character is a digit, the is_valid flag is set to False and the loop finishes immediately.

Exercise

Ask the user to provide a number from 2 to 1000 (inclusive). If the number is not correct, write: Incorrect number! and end the program. If the number is correct, check if it is a prime number (i.e., it is not divisible by anything except 1 and itself). If the number is prime, write: {user's number} is prime!.

Otherwise, write: {user's number} is not prime!.

If the number exceeds the range, print: Incorrect number!

Stuck? Here's a hint!

To check if the number x is prime, write a for loop with i in range(2, x). In each iteration, check:

if x % i == 0

If it's true, break the loop – the number is not prime.

You can use the following template to help you write the program:

user_number = int(input(...))
is_prime = True
if user_number >= 2 and user_number <= 1000:
  for i in range (2, user_number):
    ...
  if is_prime == True:
    ...
  else:
    ...
else:
  ...