While Loop¶
To repeat a code block a while
statement can be used. A while
loop is
repeated as long as a certain boolean condition is met. The condition is
(re-)evaluated before each repetition.
while condition:
CODE_BLOCK
Example:
def ask_computer(a, b):
if a > b:
return "no"
else:
return "yes"
i = 0
while i <= 3:
computer_says = ask_computer(i, 2)
print("Computer says", computer_says)
i = i + 1
# iterating over the elements of a list
i = 0
checks = [2, 0, 3, 1]
while i < len(checks):
check_value = checks[i]
computer_says = ask_computer(check_value, 2)
print("Computer says", computer_says)
i = i + 1