Reading from the Console

Workshop Resources

Now that we know the basics about variables, we can ask for input from the user. Use the following line to tell our program to wait until the user types something into the console. Then, the computer takes whatever is typed into the console, and stores it into the variable called value.

value = input()

Here’s and examples of how to use input() to accept user input:

print("What's your name?")
value = input()
print("Hello " + value + "!")

Give this a try! When you hit run, you’ll notice that the below image doesn’t appear on the console yet.

Screenshot of the console end symbol

Screen shot of the console waiting for user input

This is because the program is waiting for you to type something! Enter your name or ‘Nuvi’ into the console on the right, press Enter, and check that it prints out correctly.

Screen shot of the console after the user has given input and the program completed

Even if we type in a number, such as 8, the variable value will contain the string "8". Be careful when attempting to do math on input variables!


Let’s see if we can write a program that first prints the following two lines to console:

How can I help you today?

Afterwards, the console waits for user input. After the user types something in the console and presses Enter, the computer prints out:

You asked: [input]?
I don’t know the answer to [input]. Goodbye!

[input] should be replaced with whatever the user had typed into the console. For example, if you wanted to ask “How old am I?”, the computer will print out that it doesn’t know the answer to that.

This is very similar to the example that we went through together above. Try making some small changes from what we have already given you.