5

Python get input from user

 2 years ago
source link: https://dev.to/baransel/python-get-input-from-user-k3g
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Sign up to my newsletter!.

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)

Enter fullscreen mode

Exit fullscreen mode

Output:

Enter your name: Baransel
Baransel

Enter fullscreen mode

Exit fullscreen mode

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)

Enter fullscreen mode

Exit fullscreen mode

Output:

Enter the first number: 52
Enter the second number: 45
Total: 5245

Enter fullscreen mode

Exit fullscreen mode

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)

Enter fullscreen mode

Exit fullscreen mode

format() method:

Continue this post on my blog! Python get input from user.

Sign up to my newsletter!.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK