Python bootcamp · Lab 28

Skip multiples of 3

easyFor Loops10 minLesson: For Loops

Read the question, write Python on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read n and print the integers from 1 to n that are not multiples of 3, space-separated.

continue is the tidy way to skip a loop body when a condition matches.

Input. One line: an integer n ≥ 1.

Output. Space-separated integers that are not multiples of 3.

Constraints

  • 1 ≤ n ≤ 50

Examples

Example 1
Input
10
Output
1 2 4 5 7 8 10
Hint
  1. if i % 3 == 0: continue
  2. A comprehension with if i % 3 != 0 also works.
Show correct code

Peek only after you have tried. You can still Check your own version.

n = int(input())
print(*[i for i in range(1, n + 1) if i % 3 != 0])
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.