Skip to content
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)
ifheight>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=1whilecurr_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=""whilemsg!='quit':
msg=input(prompt)
print(msg)

Using A Flag

active=Truewhileactive:
msg=input(prompt)
ifmsg=='quit':
active=Falseelse:
print(msg)

Using break to Exit a flag

whileTrue:
msg=input(prompt)
ifmsg=='quit':
breakelse:
print(msg)

Clone this wiki locally