Hàm input[]
lấy đầu vào từ người dùng và trả về nó.
Thí dụ
name = input["Enter your name: "]
print[name]
# Output:
# Enter your name: James
# James
Đầu vào []
Cú pháp của hàm input[]
là:
input[[prompt]]
Thông số đầu vào []
Hàm input[]
có một đối số tùy chọn duy nhất:
- Nhắc [Tùy chọn] - Một chuỗi được ghi vào đầu ra tiêu chuẩn [thường là màn hình] mà không cần theo dõi dòng mới - a string that is written to standard output [usually screen] without trailing newline
đầu vào [] giá trị trả về
Hàm input[]
đọc một dòng từ đầu vào [thường là từ người dùng], chuyển đổi dòng thành một chuỗi bằng cách loại bỏ dòng mới và trả về nó.
Nếu EOF được đọc, nó sẽ tăng ngoại lệ
input[[prompt]]2.
Ví dụ 1: Làm thế nào đầu vào [] hoạt động trong Python?
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
Đầu ra
Python is interesting. The inputted string is: Python is interesting
Ví dụ 2: Nhận đầu vào từ người dùng với lời nhắc
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
Đầu ra
Enter a string: Python is interesting. The inputted string is: Python is interesting
❮ Chức năng tích hợp sẵn
Thí dụ
Yêu cầu tên của người dùng và in nó:
print ['Nhập tên của bạn:'] x = input [] in ['xin chào,' + x]
x = input[]
print['Hello, ' + x]
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Hàm input[]
cho phép đầu vào của người dùng.
Cú pháp
Giá trị tham số
lời nhắc | Một chuỗi, biểu thị một thông báo mặc định trước đầu vào. |
Nhiều ví dụ hơn
Thí dụ
Yêu cầu tên của người dùng và in nó:
print ['Nhập tên của bạn:'] x = input [] in ['xin chào,' + x]
print['Hello, ' + x]
Hãy tự mình thử »
❮ Chức năng tích hợp sẵn
Cải thiện bài viết
Lưu bài viết
Hàm Python Input [] được sử dụng để lấy đầu vào của người dùng. Theo mặc định, nó trả về đầu vào của người dùng dưới dạng chuỗi. is used to take user input. By default, it returns the user input in form of a string.
Python Input [] Chức năng cú pháp
Cú pháp: Đầu vào [Nhắc] input[prompt]
- Nhắc [Tùy chọn]: Bất kỳ giá trị chuỗi nào để hiển thị dưới dạng thông báo đầu vàoany string value to display as input message
Trả về: Trả về giá trị chuỗi làm đầu vào của người dùng. Return a string value as input by the user.
Ví dụ về hàm Python Input []
Python3
input[[prompt]]4
input[[prompt]]5
input[[prompt]]6
input[[prompt]]7
input[[prompt]]8
input[[prompt]]9
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
0input[[prompt]]7
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
2# get input from user
inputString = input[]
print['The inputted string is:', inputString]
3Output:
What color is rose?: red Rose is red
Ví dụ 1: Lấy tên và tuổi của người dùng làm đầu vào và in nóthe Name and Age of the user as input and printing it
Theo mặc định, đầu vào trả về một chuỗi. Vì vậy, tên và tuổi sẽ được lưu trữ dưới dạng chuỗi.
Python
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
4input[[prompt]]5
input[[prompt]]6
input[[prompt]]7
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
8input[[prompt]]9
Python is interesting. The inputted string is: Python is interesting0____15
input[[prompt]]6
input[[prompt]]7
Python is interesting. The inputted string is: Python is interesting4
input[[prompt]]9
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
0input[[prompt]]7
Python is interesting. The inputted string is: Python is interesting8
Python is interesting. The inputted string is: Python is interesting9
Output:
Please Enter Your Name: Rohit Please Enter Your Age: 16 Name & Age: Rohit 16
Ví dụ 2: Lấy hai số nguyên từ người dùng và thêm chúng.
Trong ví dụ này, chúng tôi sẽ xem xét cách lấy đầu vào số nguyên từ người dùng. Để lấy đầu vào số nguyên, chúng tôi sẽ sử dụng int [] cùng với python input []Python input[]
Python
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
4input[[prompt]]5
input[[prompt]]6
input[[prompt]]7
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
8input[[prompt]]9
Python is interesting. The inputted string is: Python is interesting0____15
input[[prompt]]6
input[[prompt]]7
Python is interesting. The inputted string is: Python is interesting4
input[[prompt]]9
Ví dụ 2: Lấy hai số nguyên từ người dùng và thêm chúng.
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
0input[[prompt]]7
What color is rose?: red Rose is red3
What color is rose?: red Rose is red4
What color is rose?: red Rose is red5
What color is rose?: red Rose is red6
Output:
Trong ví dụ này, chúng tôi sẽ xem xét cách lấy đầu vào số nguyên từ người dùng. Để lấy đầu vào số nguyên, chúng tôi sẽ sử dụng int [] cùng với python input []
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
0input[[prompt]]
5 # get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
2input[[prompt]]
7input[[prompt]]
6input[[prompt]]
7# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
6# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
7
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
8input[[prompt]]5
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
2input[[prompt]]7
input[[prompt]]6
input[[prompt]]7
Enter a string: Python is interesting. The inputted string is: Python is interesting4
# get input from user
inputString = input['Enter a string:']
print['The inputted string is:', inputString]
7Python
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
4input[[prompt]]5
input[[prompt]]6
input[[prompt]]7
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
8input[[prompt]]9
Python is interesting. The inputted string is: Python is interesting0____15
input[[prompt]]6
input[[prompt]]7
Python is interesting. The inputted string is: Python is interesting4
input[[prompt]]9
Ví dụ 2: Lấy hai số nguyên từ người dùng và thêm chúng.
input[]
7input[]
8
# get input from user
inputString = input[]
print['The inputted string is:', inputString]
0input[]
0Output: