When number of repetitions is unknown
num = int(input('Enter an even integer: ')) while num % 2 != 0: num = int(input('Enter an even integer: '))
num = int(input('Enter an even integer: ')) while num % 2 != 0: num = int(input('Enter an even integer: '))
num
is not even
num % 2 != 0
is True
), execute body of while
-loopnum = int(input('Enter an even integer: ')) while num % 2 != 0: num = int(input('Enter an even integer: '))
num
is not even
num % 2 != 0
is True
), execute body of while
-loopwhile
-loop asks re-prompts user and updates num
num = int(input('Enter an even integer: ')) while num % 2 != 0: num = int(input('Enter an even integer: '))
num
is not even
num % 2 != 0
is True
), execute body of while
-loopwhile
-loop asks re-prompts user and updates num
num
num = int(input('Enter an even integer: '))
while num % 2 != 0:
num = int(input('Enter an even integer: '))
while
-loopswhile
-loopswhile test:
<STATEMENT>
...
<STATEMENT>
while
-loop Guarantee
while
-loops ensure a condition is False
num = int(input('Enter an even integer: ')) while num % 2 != 0: num = int(input('Enter an even integer: ')) # num is guaranteed to be even after the while-loop!
num = int(input('Enter an even integer: ')) while num % 2 != 0: num = int(input('Enter an even integer: ')) # num is guaranteed to be even after the while-loop!
num = int(input('Enter an even integer: ')) while num % 2 != 0: num = int(input('Enter an even integer: ')) # num is guaranteed to be even after the while-loop!
num = int(input('Enter an even integer: ')) while num % 2 != 0: num = int(input('Enter an even integer: ')) # num is guaranteed to be even after the while-loop!
Write a program with a while
-loop that doubles an integer until it is greater than 100.
n = int(input('Enter an integer: '))
Write a program with a while
-loop that doubles an integer until it is greater than 100.
n = int(input('Enter an integer: ')) while n <= 100: # If n <= 100, double it. n = n * 2
Write a program with a while
-loop that doubles an integer until it is greater than 100.
n = int(input('Enter an integer: ')) while n <= 100: # If n <= 100, double it. n = n * 2 # After loop: n > 100
Write a while
-loop to count how many times it takes to roll snake eyes given two fair die.
Write a while
-loop to count how many times it takes to roll snake eyes given two fair die.
# Import randint() function from Python's random module from random import randint d1 = randint(1,6) # Roll a 6-sided die d2 = randint(1,6) # Roll another 6-sided die
Write a while
-loop to count how many times it takes to roll snake eyes given two fair die.
# Import randint() function from Python's random module from random import randint d1 = randint(1,6) # Roll a 6-sided die d2 = randint(1,6) # Roll another 6-sided die count = 1 # Number of times dice have been rolled
Write a while
-loop to count how many times it takes to roll snake eyes given two fair die.
# Import randint() function from Python's random module from random import randint d1 = randint(1,6) # Roll a 6-sided die d2 = randint(1,6) # Roll another 6-sided die count = 1 # Number of times dice have been rolled while not (d1 == 1 and d2 == 1):
Write a while
-loop to count how many times it takes to roll snake eyes given two fair die.
# Import randint() function from Python's random module from random import randint d1 = randint(1,6) # Roll a 6-sided die d2 = randint(1,6) # Roll another 6-sided die count = 1 # Number of times dice have been rolled while not (d1 == 1 and d2 == 1): # Roll dice again d1 = randint(1,6) d2 = randint(1,6)
Write a while
-loop to count how many times it takes to roll snake eyes given two fair die.
# Import randint() function from Python's random module from random import randint d1 = randint(1,6) # Roll a 6-sided die d2 = randint(1,6) # Roll another 6-sided die count = 1 # Number of times dice have been rolled while not (d1 == 1 and d2 == 1): # Roll dice again d1 = randint(1,6) d2 = randint(1,6) # Update count count = count + 1
Write a while
-loop to count how many times it takes to roll snake eyes given two fair die.
# Import randint() function from Python's random module from random import randint d1 = randint(1,6) # Roll a 6-sided die d2 = randint(1,6) # Roll another 6-sided die count = 1 # Number of times dice have been rolled while not (d1 == 1 and d2 == 1): # Roll dice again d1 = randint(1,6) d2 = randint(1,6) # Update count count = count + 1 # d1 == 1 and d2 == 1 (snake eyes!) print(count)
# Import randint() function from Python's random module from random import randint d1 = randint(1,6) # Roll a 6-sided die d2 = randint(1,6) # Roll another 6-sided die count = 1 # Number of times dice have been rolled while not (d1 == 1 and d2 == 1): # Roll dice again d1 = randint(1,6) d2 = randint(1,6) # Update count count = count + 1 # d1 == 1 and d2 == 1 (snake eyes!) print(count)
WARNING: This loop theoretically could never end…
n = 0 while n >= 0: n = n + 1 print(n)
n = 0 while n >= 0: n = n + 1 print(n)
The loop condition is always True
!
while True:
# Will greet the world, over and over, forever...
print('Hello, World!')
"Stop asking when user enters 0."
n = int(input('Enter an int: ')) while ___: n = int(input('Enter an int: '))
"Keep asking until user enters 0."
n = int(input('Enter an int: ')) while ___: n = int(input('Enter an int: '))
"Stop asking when user enters 0."
n = int(input('Enter an int: ')) while n != 0: n = int(input('Enter an int: '))
"Keep asking until user enters 0."
n = int(input('Enter an int: ')) while n != 0: n = int(input('Enter an int: '))
while
-loops repeat while the condition is True
Continuation Condition
Check whether to continue looping
Stopping Condition
Check whether to stop looping
x
is greater than 100."
Stopping Condition:
x > 100
Continutation Condition:
x <= 100
not (x > 100)
x
is greater than 0 and less than 10"
Stopping Condition:
x > 0 and x < 10
Continuation Condition:
x <= 0 or x >= 10
not (x > 0 and x < 10)
while
-loops use the continuation condition!while
vs. for
for
-loops are while
-loopsSum the first 10 positive integers.
for
s = 0 for n in range(1,11): s = s + n
while
s = 0 n = 1 # Need to initialize n! while n < 11: s = s + n # Need to increment n! n = n + 1
s = 0 n = 1 # Need to initialize n! while n < 11: s = s + n # Need to increment n! n = n + 1
These loops are equivalent!
for
vs. while
for
while
for
-loopwhile
-loopwhile
-loop?"""
Returns the number of times a user takes to guess a randomly
generated int between 1 and 100.
Prints feedback for the user after each guess. Prints message indicating
correct guess.
"""
"""
Returns the number of times a user takes to guess a randomly
generated int between 1 and 100.
Prints feedback for the user after each guess. Prints message
indicating correct guess.
"""
num_guesses
)num_guesses
while
-loop Checklist
while
-loop: repeats body while condition
is True
while condition:
<STATEMENT>
...
<STATEMENT>
for
vs. while
loops