Getting User Input

To get input from the user, Python provides a function called input().  Input does no error checking, it just takes whatever the user provides.  In this course, we’re going to use a friendlier function called get_string(), when we expect a string from the user.

The get_string() function is not part of Python, its in a library, or module, called cs50.  To bring it into our program, we need to write

at the top of our program.  In a future unit we’ll take a closer look at the import statement.

The way we use get_string is almost always to ask the user a question, and then assign the value provided to a variable.  Here is an example.

In this case the user’s response is saved in the variable called “name”, and you can use that value in a future print statement.

On your own computer

If you’ve downloaded Python yourself onto your computer, or if your computer came with Python installed, you’ll need to install the cs50 module yourself before you can import the get_string() function into your program.   In a terminal window, try this command:

If that doesn’t work, you might not have the pip package manager installed – ask me in class and I’ll help you.