Skip to content

Chapter 7

Jarlin Almanzar edited this page Nov 21, 2019 · 3 revisions

User Input and While Loops

Input()

The function pauses your program and waits for the user to enter some text. Once it retrieves the input, it assigns it to a variable.
Python interprets Input values as a string

height = input("How tall are you, in inches: ")
height = int(height)

if height > 48:
    print("You are tall enough")
else:
    print("\n You'll be able to ride when you're a little older.")

While Loop

A loop that runs as long as the condition is true.

curr_num = 1
while curr_num <= 5:
    print(curr_num)
    curr_num+=1

Let the user quit

prompt = "Tell me something, and I will repeat it back to you."
prompt += "\n Enter 'quit' to end the program."
msg = ""
while msg != 'quit':
    msg=input(prompt)
    print(msg)

Using A Flag

active = True
while active:
   msg = input(prompt)
   
   if msg == 'quit':
        active = False
   else:
        print(msg)

Using break to Exit a flag

while True:
   msg = input(prompt)
   
   if msg == 'quit':
        break
   else:
        print(msg)
Clone this wiki locally