Use of input/raw_input in python 2 and 3 [duplicat

2019-03-22 13:03发布

This question already has an answer here:

I would like to set a user prompt with the following question:

save_flag is not set to 1; data will not be saved. Press enter to continue.

input() works in python3 but not python2. raw_input() works in python2 but not python3. Is there a way to do this so that the code is compatible with both python 2 and python 3?

6条回答
女痞
2楼-- · 2019-03-22 13:22

Bind raw_input to input in Python 2:

try:
    input = raw_input
except NameError:
    pass

Now input will return a string in Python 2 as well.


If you're using six to write 2/3 compatible code then six.input() there points to raw_input() in Python 2 and input() in Python 3.

查看更多
乱世女痞
3楼-- · 2019-03-22 13:33

You can write your code in either python2 and use futurize or in python3 and use pasteurize. This removes the complexity of thinking about compatible code and guarantees good practices.

Regarding this specific question

from builtins import input

Is exactly what the above scripts produce.

查看更多
来,给爷笑一个
4楼-- · 2019-03-22 13:33

I think the best way to do this is

import six

six.moves.input()

...it'll work across 2 and 3.

查看更多
Viruses.
5楼-- · 2019-03-22 13:40

Explicitly load the function:

from builtins import input

Then you can use input() in python2 as well as python3.

You may have to install the dependency:

pip install future

查看更多
闹够了就滚
6楼-- · 2019-03-22 13:40

Update: This method only works if you have future installed and the answers above are much better and more generalizable.

From this cheatsheet there is another method that looks cleaner:

# Python 2 and 3:
from builtins import input
查看更多
我想做一个坏孩纸
7楼-- · 2019-03-22 13:46

This is because, In python 2, raw_input() accepts everything given to stdin, as a string, where as input() preserves the data type of the given argument (i.e. if the given argument is of type int, then it will remain as int only, but won't be converted to string as in case of raw_input()). That is basically, when input() is used, it takes the arguments provided in stdin as string, and evaluates the same. And this evaluation converts the argument to corresponding type.

# Python 2.7.6
>>> a = raw_input("enter :- ")
enter :- 3
>>> type(a)     # raw_input() converts your int to string
<type 'str'>
>>> a = input("enter :- ")
enter :- 3
>>> type(a)    # input() preserves the original type, no conversion     
<type 'int'> 
>>>

Therefore, while using input() in Python 2, user has to be careful while passing the arguments. If you are passing a string, you need to pass it with quote ( since python recognizes characters inside quote as string). Else NameError will be thrown.

 >>> a = input("enter name :- ")
 enter name :- Derrick
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<string>", line 1, in <module>
 NameError: name 'Derrick' is not defined
 >>> a = input("enter name :- ")
 enter name :- 'Derrick'
 >>> a
 'Derrick'

Whereas, if using raw_input(), you need not worry about the data type while passing the argument as everything it accepts as a string. But yes, inside your code you need to take care of appropriate type conversion.

To avoid this extra care needed for input() in Python 2, it has been removed in Python 3. And raw_input() has been renamed to input() in Python 3. The functionality of input() from Python 2 is no more in Python 3. input() in Python 3 serves what raw_input() was serving in Python 2.

This post might be helpful for a detailed understanding.

查看更多
登录 后发表回答