While-loops and user input

A cursor that blinks in a text editor is an example of a (potentially) infinite loop: it will blink forever if the user doesn't enter input.

Let's try to emulate that behaviour:

In [1]:
user_input = None

#keep asking a question until the user says yes
while user_input != "yes":
    user_input = input("Have you started Project 1 yet?")
Have you started Project 1 yet?hmm
Have you started Project 1 yet?say what?
Have you started Project 1 yet?no
Have you started Project 1 yet?yes

While user_input hasn't been assigned the value "yes" (because that's what the user types in and the function input() returns, so that it's assigned to user_input), the line

user_input = input("Have you started Project 1 yet?")

keeps running.

The process is

  • Check whether user_input is equal to "yes". If it is, stop the loop
  • user_input = input("Have you started Project 1 yet?")
  • Check whether user_input is equal to "yes". If it is, stop the loop
  • user_input = input("Have you started Project 1 yet?")
  • Check whether user_input is equal to "yes". If it is, stop the loop
  • user_input = input("Have you started Project 1 yet?")
  • Check whether user_input is equal to "yes". If it is, stop the loop
  • user_input = input("Have you started Project 1 yet?")
  • ...

Above, a sample run is shown. Since the loop stopped, it must be the case that user_input is not "yes". Indeed, it is.

In [2]:
user_input
Out[2]:
'yes'

An alternative way to write the same loop is:

while input("Have you started P1?") != "yes":
    pass