Trong Python 3, raw_input[]
không tồn tại mà Sven đã đề cập.
Trong Python 2, hàm input[]
đánh giá đầu vào của bạn.
Example:
name = input["what is your name ?"]
what is your name ?harsha
Traceback [most recent call last]:
File "", line 1, in
name = input["what is your name ?"]
File "", line 1, in
NameError: name 'harsha' is not defined
Trong ví dụ trên, Python 2.x đang cố gắng đánh giá Harsha như một biến chứ không phải là một chuỗi. Để tránh điều đó, chúng tôi có thể sử dụng các trích dẫn kép xung quanh đầu vào của chúng tôi như "Harsha":
>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
raw_input[]
Chức năng Raw_Input [] `không đánh giá, nó sẽ chỉ đọc bất cứ điều gì bạn nhập.
Example:
name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
Example:
name = eval[raw_input["what is your name?"]]
what is your name?harsha
Traceback [most recent call last]:
File "", line 1, in
name = eval[raw_input["what is your name?"]]
File "", line 1, in
NameError: name 'harsha' is not defined
Ví dụ ở trên, tôi chỉ cố gắng đánh giá đầu vào của người dùng với chức năng eval
.
Các nhà phát triển thường có nhu cầu tương tác với người dùng, để lấy dữ liệu hoặc cung cấp một số loại kết quả. Hầu hết các chương trình ngày nay sử dụng hộp thoại như một cách yêu cầu người dùng cung cấp một số loại đầu vào. Trong khi Python cung cấp cho chúng tôi hai chức năng sẵn có để đọc đầu vào từ bàn phím.
- đầu vào [nhắc]
- RAW_Input [Nhắc]
hàm input []
Hàm Python Input [] được sử dụng để lấy các giá trị từ người dùng. Hàm này được gọi để nói với chương trình dừng lại và chờ người dùng nhập các giá trị. Nó là một chức năng tích hợp. Hàm input [] được sử dụng trong cả phiên bản Python 2.x và Python 3.x. Trong Python 3.x, hàm đầu vào chuyển đổi rõ ràng đầu vào bạn cung cấp cho chuỗi loại. Nhưng hàm đầu vào Python 2.x lấy giá trị và loại đầu vào bạn nhập như không cần sửa đổi loại. & NBSP;
Chương trình ví dụ trong Python3 & NBSP;
Python3
val1
=
input
>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
0>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
2>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
3>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
4>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
6>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
7=
input
name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
0>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
2>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
3name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
4>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
7=
name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
7name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
8>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
2>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
3name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
4>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
8Đầu vào và đầu ra
Ở đây, giá trị của Python3, nhận được từ người dùng và lưu trữ nó trong biến Val1. Loại giá trị được lưu trữ luôn luôn là chuỗi cho hàm đầu vào chỉ cho Python 3.x. Giá trị của 1997 1997 nhận được từ người dùng và lưu trữ nó trong biến Val2. Bây giờ, loại biến Val2 là một chuỗi và chúng ta phải chuyển đổi loại thành một số nguyên bằng hàm int []. Biến Val2 lưu trữ giá trị của 1997 1997 như một loại số nguyên. & NBSP;val1 variable. The type of the value stored is always string for input function only for Python 3.x. The value “1997” take from the user and store it in the variable val2. Now, the type of variable val2 is a string and we have to convert the type to an integer using int[] function. The val2 variable stores the value “1997” as an integer type.
Chương trình ví dụ trong Python2 & NBSP;
Python3
val1
=
input
>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
0>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
2>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
3>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
4>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
6>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
7=
input
name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
0>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
2>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
3name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
4Ở đây, giá trị của Python3, nhận được từ người dùng và lưu trữ nó trong biến Val1. Loại giá trị được lưu trữ luôn luôn là chuỗi cho hàm đầu vào chỉ cho Python 3.x. Giá trị của 1997 1997 nhận được từ người dùng và lưu trữ nó trong biến Val2. Bây giờ, loại biến Val2 là một chuỗi và chúng ta phải chuyển đổi loại thành một số nguyên bằng hàm int []. Biến Val2 lưu trữ giá trị của 1997 1997 như một loại số nguyên. & NBSP;
Đầu vào và đầu ra
Chương trình ví dụ trong Python2 & NBSP;val1 variable. The function takes the value and type of the input you enter as it is without modifying the type. The type of value in val1 is string type. The value “1997” takes from the user and store it in the variable val2. Now, the type of variable val2 is integer type. We don’t need to explicitly change the variable type.
hàm raw_input []
Chức năng Python Raw_Input được sử dụng để lấy các giá trị từ người dùng. Chúng tôi gọi chức năng này để nói với chương trình dừng lại và chờ người dùng nhập các giá trị. Nó là một chức năng tích hợp. Hàm đầu vào chỉ được sử dụng trong phiên bản Python 2.x. Python 2.x có hai chức năng để lấy giá trị từ người dùng. Đo đầu tiên là hàm đầu vào và một hàm khác là hàm raw_input []. Hàm raw_input [] tương tự như hàm input [] trong python 3.x. Các nhà phát triển được khuyến nghị sử dụng chức năng RAW_Input trong Python 2.x. Bởi vì có một lỗ hổng trong hàm đầu vào trong phiên bản Python 2.x. & Nbsp;used only in Python 2.x version. The Python 2.x has two functions to take the value from the user. The first one is input function and another one is raw_input[] function. The raw_input[] function is similar to input[] function in Python 3.x. Developers are recommended to use raw_input function in Python 2.x. Because there is a vulnerability in input function in Python 2.x version.
Chương trình ví dụ trong Python2 & NBSP;
Python3
val1
=
input[]
7
>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
0>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
2>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
3>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
4>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
6>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
7=
input[]
7name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
0>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
2>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
3name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
4>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
7=
name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
7name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
8>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
2>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
3name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
4>>> name = input["what is your name?"]
what is your name?"harsha"
>>> print[name]
harsha
1name = raw_input["what is your name ?"]
what is your name ?harsha
>>> name
'harsha'
8Đầu vào và đầu ra
Ở đây, giá trị của Python3, nhận được từ người dùng và lưu trữ nó trong biến Val1. Loại giá trị được lưu trữ luôn luôn là chuỗi cho chức năng RAW_Input. Giá trị của 1997 1997 nhận được từ người dùng và lưu trữ nó trong biến Val2. Bây giờ, loại biến Val2 là một chuỗi và chúng ta phải chuyển đổi loại thành một số nguyên bằng hàm int []. Biến Val2 lưu trữ giá trị của 1997 1997 như một loại số nguyên.val1 variable. The type of the value stored is always string for raw_input function. The value “1997” take from the user and store it in the variable val2. Now, the type of variable val2 is a string and we have to convert the type to an integer using int[] function. The val2 variable stores the value “1997” as an integer type.
Hãy cho chúng tôi xem sự khác biệt trong một dạng bảng -:
& nbsp; | input[] | raw_input[] |
1. | hàm input [] Lấy đầu vào người dùng. | Hàm raw_input [] lấy đầu vào từ người dùng. |
2. | Cú pháp của nó là -: input[prompt] | Cú pháp của nó là -: raw_input[input] |
3. | Chỉ cần một tham số là nhắc nhở. | Chỉ cần một tham số là đầu vào. |
4. | Nó trả lại đầu vào mà nó cần. | Loại trả về của nó là của chuỗi. |
5. | Nó chuyển đổi đầu vào thành một chuỗi bằng cách loại bỏ dòng mới | Nó chỉ được giới thiệu trong phiên bản Python 2.0 |