Entering user data in Python
In the previous lessons, we were writing the code and then testing it and it executed as is, meaning that we already knew what would appear when the code was run because we were setting the values of the variables before running the program.
In this lesson, you will learn how to create a program that interacts with the user, as when you run it, the user will be asked to enter data, and after entering it, the program will process it and do something specific with it.
input()in python
To make the user able to enter data into the program while it is running, we use a ready-made function calledinput().
Every time you call this function, the Python interpreter waits for you to enter what you want from the keyboard.(Keyboard).
After completing the entry and clicking on the button, the Enterthing you entered will be returned as text in the place from which the function was calledinput().
Information about the input function
When you call the function input()even if you enter a number it will return it as text.
So in case you want the user to enter a number, you will have to convert what the function returns to a number.
Examples of user data entry in Python
In the following example, we create a program that asks the user to enter his name, and then displays it to him.
A program that asks the user to enter his name, then displays it to him in Python
# name Here we have shown a message asking the user to enter his name. The name you enter will be stored in the variable
name = input("What's your name?")
# name Here we have shown a welcome sentence based on the username that we stored in the variable a while ago
print("Nice to meet you", name)
• We will get the following result when we run the fileTestNoting that we marked the data that we waited for the program to be entered from the keyboard in yellow.
Nice to meet you alawirisaddam
In the following example, we create a program that asks the user to enter two integers, and then displays the result of their sum.
Note that we put the function input()inside the function int()so that the number the user will enter is converted to an integer before it is stored in the variable.
If we did not do this, the numbers entered by the user would be considered as texts and therefore it would cause a logical error to show the summation result.
A program that asks the user to enter two integers, and then shows him the result of adding them in Python
# a Then the number will be stored in the variable .int to type str Here we have shown a message asking the user to enter a number. The number to enter will be converted from type
a = int(input("Enter a: "))
# b Then the number will be stored in the variable .int to type str Here we have shown a message asking the user to enter a number. The number to enter will be converted from type
b = int(input("Enter b: "))
# Here we have shown the sum of the two numbers that were entered
print('a + b =', a + b)
• We will get the following result when we run the fileTestNoting that we marked the data that we waited for the program to be entered from the keyboard in yellow.
Enter b: 7
a + b = 12
In the following example, we created a program that asks the user to enter a number representing the number of elements in an array, and then asks him to enter a value for each element in the array.
In the end, the program shows him every value he entered on one line.
A program that asks the user to enter a number representing the number of elements in an array, and then asks him to enter a value for each element in the array.
# n Then the number will be stored in the variable .int to type str The number to enter will be converted from type .aList Here we have shown a message asking the user to enter the number of elements of the object
n = int(input("Enter the length of the list: "))
# The number of its elements is equal to the number entered by the user, and the initial value of each element in it is 0 aList named list here we created
aList = [0] * n
# one i and of course, at the beginning of each cycle in this loop the value of the .aList variable starting from 0 will be incremented to an element in the for object here we created a loop
for i in range(len(aList)):
# Then it will be placed in the .int element to type str. Every cycle the user will be asked to enter a value for a new element. Then the type of the value entered will be converted from type
aList[i] = int(input('Enter aList[' + str(i) + ']: '))
print('aList contains the following values:')
# val and store it in the aList object Here we have created a loop that fetches in each cycle the value of an element of the object
for val in aList:
# add a blank space after the value is displayed instead of going down on a new line print() making the val function here we have printed the value stored in the variable
print(val, end=' ')
• We will get the following result when we run the fileTestNoting that we marked the data that we waited for the program to be entered from the keyboard in yellow.
Enter aList[0]: 10
Enter aList[1]: 20
Enter aList[2]: 30
Enter aList[3]: 40
Enter aList[4]: 50
The list contains the following values:
10 20 30 40 50