Skip to main content

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

  1. Make sure that students have a Python file open in their IDE.

  2. Tell students that we’re going to learn how to ask the user a question.

  3. Tell students to start creating a variable. Have them leave the part after the equals sign blank.

  4. Tell students to type the word “input”.

  5. Tell students to type an open parenthesis.

  6. Tell students to type a quotation mark.

  7. Tell students to type a question. Students can choose whatever question they want.

  8. Tell students to put a second quotation mark after their question.

  9. Tell students to put a closing parenthesis after their second quotation mark.

  10. Ask students to run their program in order to make sure that it works.

  11. 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

  1. Make sure that students have a Python file open in their IDE.

  2. Write this code on the board:

    user_answer = input("Are you not an elephant? ")
  3. Ask students what they think that code will do.

  4. Tell students to type the code that’s on the board on their computers.

  5. Once a student finishes copying the code from the board, tell them to run the code.

  6. Once a student runs their code, ask them if the code did what they expected it to.

  7. Explain what the code does in order to make sure that everyone understands.

  8. 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.