Python Basic Input Tutorial

We’ve seen how to make Python share information with us, but how do we share information with Python without changing the code each time we want to tell it something new?

To do this, we need to use input. When we move the mouse, type on the keyboard, or click a button, we are sending information to the computer.

In this lesson, we’ll learn how to send strings to Python.

Python Basic Input 1

Run the code in the example.

You’ll notice that nothing is outputted right away, even though we used a print statement.

Type something on the right side and press Enter (or Return)

  • Question: Based on the output of this code, what does
    input()
    do?
  • Answer: It makes Python wait for the user to type something and press Enter. If we use it with a variable, what the user wrote becomes the value of that variable when they press Enter.

Python Basic Input 2

If we put a string in the parentheses, Python will output it as a prompt for the user.

Run:

msg = input("Type your
message here, then press
print(msg)

Python Basic Input 3

Activity

Each print statement outputs on its own line. Run the following:

Bring back the “fake quote generator” you made earlier in the course. Feel free to change it up!

Replace the hardcoded values with

input()
like in the example below. Make sure to give the user a prompt!

verb = input("Type a verb and press [enter]")
noun_plural = input("Type a plural noun and press [enter]")
famous_person = input("Type the name of a famous person and press [enter]")
print("You should never " + verb + " " + noun_plural + ". Ever.")
print("- " + famous_person)

Let a friend type to fill in the blanks.

Python Basic Input 4

A Possible Answer:

huge_number = input("Write a HUGE number, then press [enter]")
cheap_item = input("What's a cheap item?")
famous_person = input("Tell me the name of a famous person.")
print("I once bought a " + cheap_item + " for " + huge_number + " dollars. Best money I ever spent.")
print("- " + famous_person)

Explanation:

This activity will help students to see how input() can make their programs interactive.