0
0
Pythonprogramming~10 mins

Counter-based while loop in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a while loop.

Python
count = 1
while count [1] 5:
    print(count)
    count += 1
Drag options to blanks, or click blank then click option'
A>=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count < 5' which stops before printing 5.
Using 'count > 5' which never runs the loop.
2fill in blank
medium

Complete the code to count down from 10 to 1 using a while loop.

Python
num = 10
while num [1] 0:
    print(num)
    num -= 1
Drag options to blanks, or click blank then click option'
A>=
B>
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'num >= 0' which would print 0 as well.
Using 'num < 0' which never runs the loop.
3fill in blank
hard

Fix the error in the code to print even numbers from 2 to 10 using a while loop.

Python
i = 2
while i [1] 10:
    print(i)
    i += 2
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 10' which stops before printing 10.
Using 'i > 10' which never runs the loop.
4fill in blank
hard

Fill both blanks to create a while loop that prints numbers from 3 to 7.

Python
num = 3
while num [1] 7:
    print(num)
    num [2] 1
Drag options to blanks, or click blank then click option'
A<=
B-=
C+=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' which excludes 7.
Using '-=' which decreases the counter and causes an infinite loop.
5fill in blank
hard

Fill all three blanks to create a while loop that prints odd numbers from 1 to 9.

Python
n = 1
while n [1] 9:
    print(n)
    n [2] [3]
Drag options to blanks, or click blank then click option'
A<=
B+=
C2
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' which excludes 9.
Using '-' instead of '+=' which decreases the counter.
Using 1 instead of 2 which prints every number, not just odd.