input()
Let's show the use of the input()
function simply by taking the name from the user.
name = input("Enter your name: ")
print(name)
Output:
Enter your name: Baransel
Baransel
As seen above, we received a name information from the user. We printed the information we received with the print()
function, which we processed in our previous lesson.
For example, let's take two numbers from the user and add them
number1 = input("Enter the first number: ")
number2 = input("Enter the second number: ")
sum = number1 + number2
print("Total: ", sum)
Output:
Enter the first number: 52
Enter the second number: 45
Total: 5245
As you can see, we got the result of 1225, it actually gave the numbers 12 and 25 written side by side, not the sum of these two numbers.
With the
input()
function, we can only get String (text) data types from the user.
In other words, it took numbers in String type, so how do we import data of Integer (integer) type?
number1 = input("Enter the first number: ")
number2 = input("Enter the second number: ")
sum = int(number1) + int(number2)
print("Total: ", sum)
format()
method:
Continue this post on my blog! Python get input from user.
Top comments (0)