0
0
Pythonprogramming~10 mins

Why while loop is needed in Python - Test Your Understanding

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 [1]:
    print(count)
    count += 1
Drag options to blanks, or click blank then click option'
Acount < 1
Bcount = 5
Ccount <= 5
Dcount > 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment '=' instead of comparison '<=' in the condition.
2fill in blank
medium

Complete the code to keep asking the user for input until they type 'exit'.

Python
user_input = ''
while [1]:
    user_input = input('Type something (or exit): ')
print('Goodbye!')
Drag options to blanks, or click blank then click option'
Auser_input > 'exit'
Buser_input != 'exit'
Cuser_input = 'exit'
Duser_input == 'exit'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment '=' instead of comparison '!=' in the condition.
3fill in blank
hard

Fix the error in the code to avoid an infinite loop that prints numbers from 1 to 3.

Python
num = 1
while num <= 3:
    print(num)
    [1]
Drag options to blanks, or click blank then click option'
Anum += 1
Bnum -= 1
Cnum = num
Dnum = 3
Attempts:
3 left
💡 Hint
Common Mistakes
Not changing the loop variable, causing an infinite loop.
4fill in blank
hard

Fill both blanks to create a dictionary of squares for numbers 1 to 5 using a while loop.

Python
squares = {}
i = 1
while [1]:
    squares[i] = i[2]2
    i += 1
Drag options to blanks, or click blank then click option'
Ai <= 5
Bi < 5
C**
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' causing missing the last number.
Using '*' instead of '**' for squaring.
5fill in blank
hard

Fill all three blanks to create a dictionary of words and their lengths for words longer than 3 letters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1]: [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Swapping keys and values.