Python language basics 12: reading user input
April 4, 2015 1 Comment
Introduction
In the previous post we looked at how to construct a while-loop and what its purpose is. In this post we’ll quickly learn how read a user’s input.
As a reminder I use the free edition of the PyCharm IDE so all the printouts will be from that environment.
Reading user input
We can use the “input” method to read the user’s input. It comes in two versions. The first version simply waits for the user to enter something in the console without a question:
promptLessInput = input() print('Your promptless input was: ' + promptLessInput)
If you run this code in PyCharm then at first it may not be obvious what’s happening. You’ll need to click in the output window, type something and press Enter:
Another version of input – an overloaded version – accepts a prompt, which can simply be a question. Consider the following example:
promptedInput = input('What have you got to say: ') print('Your prompted input is: ' + promptedInput)
If you’re new to programming then you probably won’t understand what “overloaded” means but you can safely ignore it for now.
If you run this code then you’ll see the question in the output window. You’ll need to click at a position after the question, type some input and press Enter.
So, it’s that easy to catch the user’s input in a console window in Python.
Read all Python-related posts on this blog here.
Reblogged this on Brian By Experience.