0
0
Pythonprogramming~10 mins

Infinite loop prevention 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 prevent an infinite loop by updating the loop variable.

Python
count = 0
while count < 5:
    print(count)
    count [1] 1
Drag options to blanks, or click blank then click option'
A+=
B-=
C*=
D/=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= causes the count to decrease, leading to an infinite loop.
Using *= or /= changes the count unpredictably and may not stop the loop.
2fill in blank
medium

Complete the code to stop the loop when the user types 'exit'.

Python
while True:
    command = input('Enter command: ')
    if command [1] 'exit':
        break
Drag options to blanks, or click blank then click option'
A!=
B==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using != causes the loop to break on any input except 'exit'.
Using < or > does not correctly compare strings for equality.
3fill in blank
hard

Fix the error in the loop condition to prevent an infinite loop.

Python
i = 10
while i > 0:
    print(i)
    i [1] 1
Drag options to blanks, or click blank then click option'
A-=
B+=
C*=
D/=
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing i causes the loop to never end.
Multiplying or dividing i may not reach 0 and cause infinite loops.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

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

Fill all three blanks to create a loop that counts down from 5 to 1 and stops.

Python
counter = [1]
while counter [2] 0:
    print(counter)
    counter [3] 1
Drag options to blanks, or click blank then click option'
A5
B>
C-=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting counter at 0 causes the loop to never run.
Using < in the condition causes an infinite loop.
Increasing counter causes the loop to never end.