Writing programs is much more fun when they're interactive. For example, it's hard to imagine a computer game that doesn't allow for some kind of input from the player. After all, playing gets interesting when you can actually do something.
Python Input from User
Python offers a convenient way to collect user input from the command line with the aptly named input().
Create the Script
Head over to your text editor, create a new file that you name collect.py, and add the following code:
user_input = input()
print(user_input)
Execute the Script
Save the file and execute it through your command line. The program will start running and then seemingly stop at a blank screen:
It looks like nothing is happening, but that's not true. You asked Python to open up a prompt for you to type in your input. Python is patiently waiting for you to do so.
Communicate with the Script
Give it a try. Once you type in any sort of input and press Enter, Python will print your input back to the command line:
After Python printed your message back to you, you might also notice that the terminal prompt changed back to normal. This means that your script has finished execution, and Python went on its well-deserved lunchtime break.
Nothing too world-displacing has happened here, you may think. All that Python did was copy the sentence that you typed. Not too impressive, Python. But that's just because printing a copy was the only thing that you asked Python to do with your input.
Python Get User Input Practice
- Change the script to apply a string method to your input. For example, let Python repeat everything you say in ALL CAPS.
- Then, make Python repeat only every second character from your input.
The subtle input() opens up the gates to your Python script so you can flood it with your personal input!
But while you, as the programmer, might remember what to type when Python goes blank and starts waiting for you, anyone else will probably think that your program died. And even if they trust your skills as a programmer, they will still not know what they are supposed to type in here! Luckily, input() takes an argument that can help with that.
Python User Input Prompt Message
You can add a string in between the parentheses of input(), and Python will display that message while waiting for your input.
Add Message Parameter
Improve your script by adding an explanatory sentence:
user_input = input("Enter something for Python to echo back: ")
print(user_input)
Save and run your script another time. Now Python will display the input prompt and make your program more understandable:
Whatever text you add as an argument to input() will be displayed when you run the program.
Prompt Practice
- Add descriptive input prompts to the string-altering programs you wrote in the previous task.
- Create a sarcastic program that asks a user for their honest opinion, then prints the same sentence back to them in aLtErNaTiNg CaPs.
Build a Calculator with Input Prompts
Next, think back to using Python as your calculator. You could simply type numbers and operators into your console, and Python evaluated the calculations for you. What if you wanted to create a calculator that displays nice input prompts:
number = input("Enter a number you want squared: ")
print(number ** 2)
Save and run the file, then give it a spin. Whoops! Not quite what you might have expected:
You managed to get Python quite confused, and it lets you know about that with a helpful error message:
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
Input Function Returns a String
The reason you receive this error is that anything that comes in through input() will be a string. So even if you enter a number, such as 4, it will arrive in your program as the string "4".
In a future lesson, you'll revisit type conversion to make sure that your collected user input will have the right data type for your program to work with.
Summary: Python User Input
- You can collect user input with
input() input()also takes a prompt string as an argument- Python will display the prompt string before the input prompt
- Any user input collected with
input()will be a string - The input collected from a user can be saved in a variable and used in your program