Card 3: Input

Module 1: The Basics

1 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 in a variable.

2 Worked Example

See how we ask a question and save the answer.

name = input("Who are you? ") # Ask question, wait, and save answer in 'name'
print("Hello " + name) # Join "Hello " with the answer

PREDICT

If I type "Red", what happens?

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

Interactive Editor

> _
BRONZE (MODIFY)

🥉 Change the Question

1. Copy the code below.
2. Change the question to ask for their Favourite Food (instead of City).
3. Change the print message to say "I love [food] too!".

city = input("Which city? ")
print("I visited " + city)
> _
SILVER (FIX)

🥈 Broken Input

The code below is trying to store the input into the variable name, but it's written backwards.
Task: Fix it.

> _
GOLD (MAKE)

🥇 Security System

Build a simple security checker using 2 inputs:

  1. Ask for "Enter New PIN: " and store in pin1.
  2. Ask for "Confirm PIN: " and store in pin2.
  3. Print "System Armed" at the end.
> _