Hướng dẫn difference between raw_input() and input in python 3 - sự khác biệt giữa raw_input () và đầu vào trong python 3

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
3
name = 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'
7
name = 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
3
name = 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
1
name = 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
3
name = 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[]7
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
3
name = 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'
7
name = 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
3
name = 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
1
name = 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ớiNó chỉ được giới thiệu trong phiên bản Python 2.0

Raw_input [] làm gì trong Python?

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 Python 2.get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function. The input function is used only in Python 2.

Làm thế nào để in [] khác với input [] trong python?

Chức năng in hiển thị thông báo đã cho trên màn hình. Ví dụ. In ["Xin chào"] cho đầu ra vì Hello trong khi hàm đầu vào chấp nhận dữ liệu đã cho. Ví dụ: Input = "Nhập tuổi của bạn:" Hiển thị đầu ra khi nhập tuổi của bạn và khi bạn vào tuổi của bạn và nhấn nhập vào tuổi của bạn.print ["Hello"] gives output as Hello Whereas input function accepts given data. For example, input = "Enter your age :" shows output as Enter your age and when you enter your age and press enter it takes in your age.

Đầu vào [] có được hỗ trợ trong Python 3 không?

Chức năng Python 3 - Input [] Trong python, chúng tôi sử dụng hàm input [] để lấy đầu vào từ người dùng.Bất cứ điều gì bạn nhập làm đầu vào, hàm đầu vào chuyển đổi nó thành một chuỗi.Nếu bạn nhập hàm INPUT [] giá trị số nguyên, hãy chuyển đổi nó thành một chuỗi.In Python, we use input[] function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input[] function convert it into a string.

Việc sử dụng hàm input [] và print [] là gì?

Hàm input [] giúp nhập dữ liệu theo thời gian chạy của người dùng và hàm đầu ra in [] được sử dụng để hiển thị kết quả của chương trình trên màn hình sau khi thực thi.helps to enter data at run time by the user and the output function print [ ] is used to display the result of the program on the screen after execution.

Bài Viết Liên Quan

Chủ Đề