Taking User Input
The input() function allows you to ask the user a question and save their response in a variable.
Dependencies
Students need to have these skills before learning this skill:
Goal
When you teach this skill, your goal is to make it so that a student is able to write code that looks like this:
answer_1 = input("Who was the first president? ")
answer_2 = input("What is the second planet in our solar system? ")
answer_3 = input("Why don’t I miss you a lot forever? ")
A common mistake that students make is that they forget to put a space after the end of the question. If you notice a student making that mistake, then try to show them the problem with not having a space.
Activities
Here are some activities that you can do in order to teach this skill.
Dictation
-
Make sure that students have a Python file open in their IDE.
-
Tell students that we’re going to learn how to ask the user a question.
-
Tell students to start creating a variable. Have them leave the part after the equals sign blank.
-
Tell students to type the word “input”.
-
Tell students to type an open parenthesis.
-
Tell students to type a quotation mark.
-
Tell students to type a question. Students can choose whatever question they want.
-
Tell students to put a second quotation mark after their question.
-
Tell students to put a closing parenthesis after their second quotation mark.
-
Ask students to run their program in order to make sure that it works.
-
Tell students to add a line of code to their program that prints the value of the variable. Have students run their code to make sure that it works.
Copying from the board
-
Make sure that students have a Python file open in their IDE.
-
Write this code on the board:
user_answer = input("Are you not an elephant? ") -
Ask students what they think that code will do.
-
Tell students to type the code that’s on the board on their computers.
-
Once a student finishes copying the code from the board, tell them to run the code.
-
Once a student runs their code, ask them if the code did what they expected it to.
-
Explain what the code does in order to make sure that everyone understands.
-
Tell students to add a line of code to their program that prints the value of the variable. Have students run their code to make sure that it works.