❮ 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]
Đị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 Google9 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àoThê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]
❮ 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]
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]
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 Google9 và tiếp tục khi người dùng nhập một số thông tinHã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 Google11,
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google12 và
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google13 để 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 Google1
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google2
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google3
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google4
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google5
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google6
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google7
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google8
What is your name? Ram Ram0
What is your name? Ram Ram1
What is your name? Ram Ram2
đầu ra
Lấy Chuỗi làm đầu vào
Python3
What is your name? Ram Ram3
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google5
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google6
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google7
What is your name? Ram Ram7
What is your name? Ram Ram8
What is your name? Ram Ram9
What is your name? Ram Ram1____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 Google12
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google13
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google3
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google15
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google5
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google6
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google7
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google19
What is your name? Ram Ram0
What is your name? Ram Ram1____222
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google23
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google5
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google6
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google7
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google27
What is your name? Ram Ram0
_______41____230
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google3
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google32
What is your name? Ram Ram1
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google7
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google35____236
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google37
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google38
What is your name? Ram Ram1
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google7
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google41
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google36
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google37
Enter Employee Name: Jessa Enter salary: 8000 Enter Company name: Google Printing Employee Details Name Salary Company Jessa 8000 Google44
đầ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ữ