Hướng dẫn python get keyboard input
Get user input with Python using the Show Old versions of Python used the now deprecated The goals of this article are:
Related Course: Complete Python Programming Course & Exercises Syntax of input() functionThe syntax of the input("Your name: ") As parameter it takes a string, that is printed in the terminal. The parameter is a text printed to the screen. If we speak about text we say string. You can print information to the user, i.e. what value they should enter. The input() function:
Get User Input in PythonHere is a simple example that gets user input and prints it in the terminal. name = input("Enter a name: ") The input() function gets user input (keyboard) and stores it into variable name. Here name is a variable. The print() function shows it to the screen. The user must press the enter key in the command line. Then the entered string is send to your application. So, to get a text value you can use the function
You can use fancy output named formatted strings or
f-strings. name = input("Enter your name: ") In summary:
You can now give keyboard input, it will be stored in the variable name. Related Course: Complete Python Programming Course & Exercises Return typeAny value you enter to See the example below in the Python shell: >>> name = input("Enter name: ") Numbers do not have the type >>> x = 3 How to get an Integer as the User Input?If you call the To get an integer (whole number), you can do this:
Get integer user input on a single line: x = int(input("What is x? ")) You can get multiple variables from the user, and use them in your program. x = int(input("Enter x: ")) Take into account that if the user doesn’t actually enter an integer, this code will throw an exception. >>> x = int(input("What is x? ")) So make sure to enter a number. If you want to prevent exceptions, see the section below Input Exception Handling. Read input as floatTo get a number (non-integer), like a floating point number, you can call the A float (floating
point) is a number like x = float(input("Write a number")) The input must be a floating point, any other input will throw an exception. See Input Exception Handling. Python user input and EOFError ExampleThe
program can have an EOFError. This exception is raised if the If you have a program like this: val = input("Enter a value: ") You can interrupt the program by pressing $ python3 example.py Python User Input Choice ExampleYou can build a multiple choice input system. Then you can evaluate the choice by using the if-elif-else structure.
choice = input("Enter A, B or C: ") It’s important to use 4 spaces with every indent, not tabs and not a different amount of spaces. Input Exception HandlingIf the user enters invalid input or invalid input, Python can throw an exception. To handle this, you can use the code below: x = 0 raw_input() - old versionsIn Python 3 and newer you can use the
The difference between this functions is zero, only the version name. While it’s the same functionality but you should use the So instead: name = input("Enter name: ") The ConclusionYou can take user input with the input() function. This waits for keyboard input indefinetly. If you add a parameter, it will print that text before the user input. You also saw how to handle invalid input and know about the difference between Python 2 and Python 3 (and newer). If you are new to Python, I recommend the course below. Related Course: Complete Python Programming Course & Exercises Download exercises |