Kids Coding
Ask a Question
input() waits for a typed answer. Save it. Print it back.
Ask, then use the answer
input() waits for someone to type. What they type comes back as a string. Save it in a variable.
In the browser editor, input() can be awkward. Practice with a saved answer first. Same idea: a string in a box.
A saved answer (runs here)
name = "Sam"
print("Hello, " + name)The real input() line
When you run Python on a computer, or when the editor asks you, this is the shape:
Ask for a name
name = "Sam"
# On a computer you can write:
# name = input("What is your name? ")
print("Nice to meet you, " + name)input("What is your name? ") shows the question, then waits. The space at the end keeps the typing off the question.
Answers are strings
Even if someone types 7, input() gives you the string "7". For maths, convert with int("7").
A number from text
typed = "7"
age = int(typed)
print("Next year:", age + 1)Real life
Asking a friend what pizza they want.
You wait for an answer, then use it. That is input — here the answer is already saved so it runs in the editor.
Pizza order
friend = "Riley"
pizza = "mushroom"
print(friend + " wants " + pizza + " pizza.")
print("Coming right up.")Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Put a colour in a variable called colour. Print I like and that colour.
Show solution
colour = "blue"
print("I like " + colour)