The Python input() function - Reading Keyboard data

So far, in our Python Course, our scripts have no interaction between Python and the user.
They simply run from start to finish, always the same way.

But that is not what happens routinely in the programs we use.
We provide data (such as texts, login, passwords), click on things, receive data from the internet, etc.

In this tutorial of our course, we will teach you how to start receiving data from the people who are running the programs, through the Python input() function!



How to receive data -Python  Function input()

The format of the input function is as follows:

  • variable = input(string)


Only that.

Whatever the person types, the information will be stored in the variable 'variable'. And what will be displayed on the screen is the string (text) 'string'.

Let us see in practice the use of the input function. Program the following code:

  • variable = input ('Type something:')


The result of it will be:

Input function in Python
Ready.

Since we supplied the string 'Type something:' for the input function, that's exactly what was displayed on the screen (Digite algo: in portuguese).

Then the Python interpreter simply stands still, waiting for you to type something. As long as you do not hit enter, nothing will happen.

When you press enter, it continues.
In the case, our script is only used to store what we type in the variable 'variable', in the form of a string.

Warning: the input function stores in string form if you are using the recent version of Python 3.x  ok?

If it is old version, it will transform your data into string, integer or float, depending on what you type.
Update your Python! Use the newest version!


input() Function exercise in Python

"Make a program that asks the user's age, and stores it in a variable, then asks the person's name and stores that data in another variable. Finally, display a welcome message to the Progressive Python course, saying name and age of the person ".

Initially, we will store the age of the user in the variable 'age', and we use the input function to receive such data.

Then we will do the same with the name, storing in the variable 'name'.

Finally, we give a print where we write a greeting message and also print the name and age of the person, which are stored in the variables 'name' and 'age', see how simple it was:

age=input('Your age:')
name=input('Your name:')

print('Hello, your name is', name, ' and have ', age, ' years old! Welcome to Progressive Python Tutorials')

The result (portuguese) is

How receive data from keyboard user in Python


Note that we type the name "Bruce Dickinson" in quotation marks, this is necessary if you are using an older version of Python, if you do not use it, you will get an error message.

If you are in the latest version ( at the moment I am writing this tutorial for our course, it is 3.6), you do not have to use quotation marks, the input function passes everything to string.

If you want to type a string without the need for quotation marks, in older versions of Python, instead of input use raw_input

Okay, finally you're communicating with Python.
He waits, waits, sits quietly and eagerly waiting for you to give the orders. You're the boss on the whole! After all, you are the programmer, also known as the 'owner of the universe'.

With big powers come big responsabilities.
In the next tutorials we will create a calculator in Python. Feel the power in your hands.

No comments:

Post a Comment