Tutorial 3/15 The Basics

Python: Input

Interact with your users by capturing data from the keyboard.

The Hook

If print() is the Megaphone (Output), then input() is the Microphone (Input).

It pauses the program and opens a "listening ear" to wait for the user to type something. Whatever they type gets stored instantly in a variable box.

1
Program
Pauses
2
Data
Captured

Question & Answer

See how we ask a question and save the user's response.

name = input("Who are you? ")
# Ask question, wait for answer
print("Hello " + name)
# Join greeting with their name
Tracing

Predict

If I type "Red" when prompted, what appears?

color = input("Fave color? ")
print("I like " + color)

Live Python Lab

Interactive Input Session.

> SYSTEM READY_
> _
BRONZE

🥉 Favourite Food

Modify the code below:

  • Ask for Favourite Food (instead of City).
  • Change the output to say "I love [food] too!"
> _
SILVER

🥈 Fixed Assignment

The code below is trying to store the input, but it's backwards!

> _
GOLD

🥇 Security System

Ask for a PIN, confirm it, and enable the system.

  • Store first input in pin1
  • Store second input in pin2
  • Print "System Armed"
> _