Reading from the Console

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 an example 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 #Can’t find image doesn’t appear on the console yet. See below.

#Can’t find image

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

#Can’t find image

Note: 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!

Activity 4

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

Welcome!
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.

Hint: 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.