Input data from the user in Python - ProgrammerTech
Language:

Search

Input data from the user in Python

  • Share this:
Input data from the user in Python

Input in Python

Input In Python, we know that programming is an input and an output, and between the logic and software units.

Today we will learn how to receive Input through the keyboard using the input function, that is, how to create a program that interacts with the user and through which we make the user able to enter data into the program while the program is working and after entering it, the program will process it and do a specific thing with it. And when the input function is called, the compiler of this language waits for the user to enter the appropriate values ​​he wants to enter, and after completing the entry, we press the Enter key and it will return. Everything that the user typed into a text in the place from which the function was called. But if you want what is within the function to be an integer, the function must be set. It returns anything entered into it and considers it a number.

 

input() function in Python

It is used in order to make the user able to enter data into the program while this program is running. In this matter, we use a ready-made function called input() and every time we call the input() function, the private interpreter in Python waits for the user to enter what he wants from the panel Keys (Keyboard) and after completing the entry and clicking the Enter button, the thing that was entered will be returned.

 

Examples of data entry from the user

>>>name = input ("your name is ?")

We created a variable name and called it the input function. When I press the Enter button, the program will write to me the question and we will enter what it asked for, which is the name. After we asked the question and he wanted to enter a specific request for him, which is a name I wrote. And I pressed the Enter button. You have entered a word for the program, and to display what was entered, we write the variable print as follows

>>>name = input ("your name is ?")
your name is ?ahmed
>>>print(name)
ahmed

This is for texts, as for setting the function to receive numbers, as in this example

>>>age = int (input("your age is ?"))
age is ?26
>>>age
26

As noted, we set the function to receive numbers only with an int before a function. Input, if we type letters it will give us an error because it is set to receive numbers only.

Example

We will set the function to receive numbers and do a arithmetic operation which is adding two numbers a and b that the program asks for.

>>> a = int(input("Enter a Number: "))
Enter a Number: 23
>>> b = int(input("Enter b Number: "))
Enter b Number: 28
>>> print('a + b = ', a + b)
a + b =  51

We note that we have put the input() function inside the int() function in order to convert the number that the user will enter into an integer before storing it in the variable.

Let's take an example by writing a simple script, which I will write in pycharm.

name = input ("your name is?")
age = int (input("your age is?"))
print("your name is {0} and your age is {1}".format (name,age))

In this script, we used the format function we talked about earlier.

your name is ?ahmed
your age is ?26
your named is ahmed and your age is 26

We notice that when I run the program, it asks me the first question, and I write the name ahmed to him, and then I press Inter. He asked me the second question directly and I wrote to him the 26-year-old who asked me. And when I pressed Enter again, it showed us an entire line of the program filled with the entries it requested from me.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com