Lesson 01-08: User Input

Learning Target: I can write a program to get user input.

The best kinds of programs are often the interactive ones. Basic interactivity is easy to implement in python using the input function.

An important note: using the input function in the ActiveCode windows in the browser may have unintended side effects. For best results (and most accurate results), I highly advise you run this code in your own text-based python IDE.

The input() Function

The built-in input() function is a way to prompt the user to type something in. In python, this would be done in the console (where the output is). On this site, it will appear as a pop-up box. Inside the parentheses, you would put the prompt as a string. The prompt is what the user sees when you are asking for input. Run the following two examples as a demonstration.

They should both do the exact same thing. Notice how we put the input() function as part of a variable assignment statement. This is because the input() function actually represents a value - the value being whatever was typed in to the prompt.

The Datatype of input()

An important question here is: what type of data does input() give you? We can easily find out using the type() function. Try running the example below.

So here, we’re asking the user Hello, what's your name? and saving their input in a variable called name. We then print the datatype of name using the type() function. The result is a string. What if you had typed in a number? Try running the example below.

Still a string! We then know that The input function will always return a string, no matter what you type in. This is important to know, because if we wanted to ask a user for a number, we would have to convert it using a conversion function before using. See the following example:

Checks for Understanding

Q#1

In the space below on line 2, write a statement that will ask the user to enter in their favorite food using input() and save it in a variable called fav_food.

Q#2

In the space below on line 2, write a statement that will ask the user to guess a number between 1 and 10 using input(), convert it to an integer, and save it in a variable called guess.

Next Section - Unit 1 Pre-Lab Check