Inputs | Python tutorial in Sinhala Learn Basic of Python Programming #7 Video
Watch the video before you read this.
Let`s talk about the input function. First, we have to open Pycharm then I type this example on Pycharm as shown in the video.
input ("Enter Number : ")
what this example. it’s said to input a number, but if you want to get the output,
You must assign it to a variable.
Now I have assigned a variable “u” as shown below,
u = input("Enter Number : ")
Then we need to output this input variable, for that, I type Print and then put a bracket inside the bracket I type the variable I got.
print(u)
Now I execute this code. Now you can see the output, then you can input any number on the terminal. I type 1 as a number. You can see the output shown in the video.
Enter Number : 1
1
Process finished with exit code 0
Here we type as enter a number within quotations because it is a string This is to make it easier for the user to recognize.
Let`s see another example for inputs. Now I type as shown in the below;
my = input("Enter Your Name Here : ")
print(my + " Welcome to Pasiya Programming. ")
I run this one. then you can input any name, I type Pawani as a name. Then you can see the output shown below.
Note: I type ” Welcome to Pasiya Programming ” I put some spaces before start typing “W’ and ending you can see that in the video.
Enter Your Name Here : pawani
pawani Welcome to Pasiya Programming.
Process finished with exit code 0
Next example, I take a and b as variables. Then I type as shown in the below
a = input("Enter First Number : ")
b = input("Enter Second Number : ")
print(a + b)
I assigned a and b for entering the first number and enter the second number. Then I type print(a + b) After I typed and run the code.
Enter First Number : 1
Enter Second Number : 3
13
Process finished with exit code 0
Here, the first value is 1 and the second value is 3 but the output is 13, but this is not the correct answer. The correct answer is 4. Why shows incorrect answers in our program? Let’s see we can`t addition strings why can`t addition strings because We did a calculation here We need this 1 + 3 = 4 answer. So what do we do? We can convert the numbers before you entered them into integers. Now let’s see how to do it. We need to code more lines to convert strings to an integer. You can simply copy the code below.
a = input("Enter First Number : ")
b = input("Enter Second Number : ")
print(type(a) + type(b))
p = int(a) + int(b)
print(p)
You can type print(type(a) + type(b)) to identify the numbers of strings you have entered, after type it then runs your program. it will show like this.
Enter First Number : 1
Enter Second Number : 3
<class 'str'><class 'str'>
Process finished with exit code 0
Then You can Convert strings to integers from the code above. Now you need to assign a variable to store our values, I got p as a variable. Then you can run the code. It will show the answer given below.
Enter First Number : 1
Enter Second Number : 3
<class 'str'><class 'str'>
<class 'int'>
4
Process finished with exit code 0
Now You can see the output. We got the answer exactly right, and you can identify that the coding We used to convert is working correctly by the output.
So that`s all We talked about in this video I hope you will learn anything from this video.
Subscribe My Channel for watch More Educational Contents.