Làm thế nào để bạn kiểm soát đầu vào trong python?

❮ Chức năng tích hợp sẵn

Thí dụ

Hỏi tên người dùng và in nó

print['Nhập tên của bạn. ']
x = input[]
print['Xin chào, ' + x]

Tự mình thử »

Định nghĩa và cách sử dụng

Hàm

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
9 cho phép người dùng nhập liệu

cú pháp

Giá trị tham số

ParameterDescriptionpromptA Chuỗi, đại diện cho một thông báo mặc định trước đầu vào

Thêm ví dụ

Thí dụ

Sử dụng tham số nhắc để viết tin nhắn trước khi nhập

x = input['Nhập tên của bạn. ']
print['Xin chào, ' + x]

Tự mình thử »

❮ Chức năng tích hợp sẵn


Ví dụ sau yêu cầu tên người dùng và khi bạn nhập tên người dùng, nó sẽ được in trên màn hình

Trăn 3. 6

tên người dùng = input["Nhập tên người dùng. "]
print["Tên đăng nhập là. " + tên người dùng]

Chạy ví dụ »

Trăn 2. 7

tên người dùng = raw_input["Nhập tên người dùng. "]
print["Tên đăng nhập là. " + tên người dùng]

Chạy ví dụ »

Python ngừng thực thi khi nói đến hàm

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
9 và tiếp tục khi người dùng nhập một số thông tin

Hãy xem cách sử dụng 
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
11, 
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
12 và 
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
13 để chứng minh đầu ra văn bản trên màn hình và bảng điều khiển

input []: This function first takes the input from the user and converts it into a string. The type of the returned object always will be . It does not evaluate the expression it just returns the complete statement as String. For example, Python provides a built-in function called input which takes the input from the user. When the input function is called it stops the program and waits for the user’s input. When the user presses enter, the program resumes and returns what the user typed. 

cú pháp

inp = input['STATEMENT']
    
Example:
1.  >>> name = input['What is your name?\n']     # \n ---> newline  ---> It causes a line break
            >>> What is your name?
            Ram
            >>> print[name]
            Ram 
            
            # ---> comment in python

Python3




Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
1

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
2

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
3

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
4
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
5
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
6
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
7
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
8
What is your name?
Ram
Ram
0

What is your name?
Ram
Ram
1
What is your name?
Ram
Ram
2

đầu ra

 

Lấy Chuỗi làm đầu vào

Python3




What is your name?
Ram
Ram
3
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
5
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
6
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
7
What is your name?
Ram
Ram
7
What is your name?
Ram
Ram
8
What is your name?
Ram
Ram
9

What is your name?
Ram
Ram
1____211

đầu ra

What is your name?
Ram
Ram

Cách hoạt động của hàm nhập liệu trong Python.
 

  • Khi hàm input[] thực thi, luồng chương trình sẽ bị dừng cho đến khi người dùng nhập xong
  • Văn bản hoặc thông báo hiển thị trên màn hình đầu ra để yêu cầu người dùng nhập giá trị đầu vào là tùy chọn i. e. lời nhắc sẽ được in trên màn hình là tùy chọn
  • Bất cứ điều gì bạn nhập làm đầu vào, hàm nhập sẽ chuyển đổi nó thành một chuỗi. nếu bạn nhập một giá trị số nguyên thì hàm input[] vẫn chuyển đổi nó thành một chuỗi. Bạn cần chuyển đổi nó một cách rõ ràng thành một số nguyên trong mã của mình bằng cách sử dụng kiểu chữ.  

Mã số.  

Python3




Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
12

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
13

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
3

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
15
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
5
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
6
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
7
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
19
What is your name?
Ram
Ram
0

What is your name?
Ram
Ram
1____222

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
23
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
5
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
6
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
7
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
27
What is your name?
Ram
Ram
0

_______41____230

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
3

Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
32

What is your name?
Ram
Ram
1
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
7
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
35____236
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
37
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
38

What is your name?
Ram
Ram
1
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
7
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
41
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
36
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
37
Enter Employee Name: Jessa
Enter salary: 8000
Enter Company name: Google

Printing Employee Details
Name Salary Company
Jessa 8000 Google
44

đầu ra.  

raw_input[]. Chức năng này hoạt động trong phiên bản cũ hơn [như Python 2. x]. Hàm này lấy chính xác những gì được nhập từ bàn phím, chuyển đổi nó thành chuỗi, sau đó trả về biến mà chúng ta muốn lưu trữ

Chủ Đề