如何在Python 3中使用raw_input

问题:如何在Python 3中使用raw_input

import sys
print(sys.platform)
print(2**100)
raw_input()

我正在使用Python 3.1,无法raw_input“冻结” dos弹出窗口。我正在阅读的书是针对Python 2.5的,而我正在使用Python 3.1

我应该怎么做才能解决这个问题?

import sys
print(sys.platform)
print(2**100)
raw_input()

I am using Python 3.1 and can’t get the raw_input to “freeze” the dos pop-up. The book I’m reading is for Python 2.5 and I’m using Python 3.1

What should I do to fix this?


回答 0

从Python 3开始,raw_input()已重命名为input()

摘自Python 3.0的新增功能,“内置”部分的第二项。

Starting with Python 3, raw_input() was renamed to input().

From What’s New In Python 3.0, Builtins section second item.


回答 1

这适用于Python 3.x和2.x:

# Fix Python 2.x.
try: input = raw_input
except NameError: pass
print("Hi " + input("Say something: "))

This works in Python 3.x and 2.x:

# Fix Python 2.x.
try: input = raw_input
except NameError: pass
print("Hi " + input("Say something: "))

回答 2

解决此问题的可靠方法是

from six.moves import input

是一个模块,可修补许多2/3通用代码基础痛点。

A reliable way to address this is

from six.moves import input

six is a module which patches over many of the 2/3 common code base pain points.


回答 3

正如其他人所指出的那样,该raw_input函数已input在Python 3.0中重命名为,确实可以得到一本更新的书来更好地服务,但我想指出的是,有更好的方法可以查看脚本的输出。

根据您的描述,我认为您正在使用Windows,已经保存了一个.py文件,然后双击它来运行它。程序结束后,弹出的终端窗口将立即关闭,因此您看不到程序的结果是什么。为了解决这个问题,您的书建议添加raw_input/input语句,直到用户按下Enter键。但是,正如您所看到的,如果出现问题(例如程序中的错误),则该语句将不会执行,并且在您看不到问题出在哪里之前,该窗口将关闭。您可能会发现使用命令提示符或IDLE更容易。

使用命令提示符

当您查看包含Python程序的文件夹窗口时,请按住shift键并右键单击窗口白色背景区域中的任意位置。弹出的菜单应包含“此处打开命令窗口”条目。(我认为这适用于Windows Vista和Windows7。)这将打开一个命令提示符窗口,如下所示:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

    C:\Users\Weeble\My Python Program>_

要运行您的程序,请键入以下内容(替换您的脚本名称):

    python myscript.py

…然后按Enter。(如果收到“ python”不是可识别的命令的错误,请参阅http://showmedo.com/videotutorials/video?name=960000&fromSeriesID=96)当程序完成运行时,无论是否成功完成,该窗口将保持打开状态,并且命令提示符将再次出现,以供您键入其他命令。如果要再次运行程序,则可以按向上箭头以调出您输入的上一个命令,然后按Enter键再次运行它,而不必每次都键入文件名。

使用IDLE

IDLE是随Python一起安装的简单程序编辑器。除其他功能外,它还可以在窗口中运行程序。右键单击您的.py文件,然后选择“在IDLE中编辑”。当您的程序出现在编辑器中时,按F5或从“运行”菜单中选择“运行模块”。程序将在程序结束后的窗口中运行,您可以在其中输入Python命令以立即运行。

As others have indicated, the raw_input function has been renamed to input in Python 3.0, and you really would be better served by a more up-to-date book, but I want to point out that there are better ways to see the output of your script.

From your description, I think you’re using Windows, you’ve saved a .py file and then you’re double-clicking on it to run it. The terminal window that pops up closes as soon as your program ends, so you can’t see what the result of your program was. To solve this, your book recommends adding a raw_input / input statement to wait until the user presses enter. However, as you’ve seen, if something goes wrong, such as an error in your program, that statement won’t be executed and the window will close without you being able to see what went wrong. You might find it easier to use a command-prompt or IDLE.

Use a command-prompt

When you’re looking at the folder window that contains your Python program, hold down shift and right-click anywhere in the white background area of the window. The menu that pops up should contain an entry “Open command window here”. (I think this works on Windows Vista and Windows 7.) This will open a command-prompt window that looks something like this:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

    C:\Users\Weeble\My Python Program>_

To run your program, type the following (substituting your script name):

    python myscript.py

…and press enter. (If you get an error that “python” is not a recognized command, see http://showmedo.com/videotutorials/video?name=960000&fromSeriesID=96 ) When your program finishes running, whether it completes successfully or not, the window will remain open and the command-prompt will appear again for you to type another command. If you want to run your program again, you can press the up arrow to recall the previous command you entered and press enter to run it again, rather than having to type out the file name every time.

Use IDLE

IDLE is a simple program editor that comes installed with Python. Among other features it can run your programs in a window. Right-click on your .py file and choose “Edit in IDLE”. When your program appears in the editor, press F5 or choose “Run module” from the “Run” menu. Your program will run in a window that stays open after your program ends, and in which you can enter Python commands to run immediately.


回答 4

Timmerman的解决方案在运行代码时效果很好,但是如果您不想Undefined name在使用pyflakes或类似的linter时出错,则可以使用以下代码:

try:
    import __builtin__
    input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
    pass

Timmerman’s solution works great when running the code, but if you don’t want to get Undefined name errors when using pyflakes or a similar linter you could use the following instead:

try:
    import __builtin__
    input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
    pass

回答 5

这是我在脚本中输入的一段代码,我不想在与py2 / 3无关的环境中运行:

# Thank you, python2-3 team, for making such a fantastic mess with
# input/raw_input :-)
real_raw_input = vars(__builtins__).get('raw_input',input)

现在,您可以使用real_raw_input。它相当昂贵,但简短易读。使用原始输入通常很耗时(等待输入),因此并不重要。

从理论上讲,您甚至可以分配raw_input而不是real_raw_input,但是可能会有一些模块检查raw_input的存在并相应地运行。最好保持安全。

Here’s a piece of code I put in my scripts that I wan’t to run in py2/3-agnostic environment:

# Thank you, python2-3 team, for making such a fantastic mess with
# input/raw_input :-)
real_raw_input = vars(__builtins__).get('raw_input',input)

Now you can use real_raw_input. It’s quite expensive but short and readable. Using raw input is usually time expensive (waiting for input), so it’s not important.

In theory, you can even assign raw_input instead of real_raw_input but there might be modules that check existence of raw_input and behave accordingly. It’s better stay on the safe side.


回答 6

可能不是最好的解决方案,但是在我来这里之前,我只是在不停学习的情况下即时进行此操作以保持工作状态。

def raw_input(x):
  input(x)

然后,当我raw_input('Enter your first name: ')在我正在处理的脚本上运行时,它会捕获它input()

可能有一个原因,我还没有遇到呢!

Probably not the best solution, but before I came here I just made this on the fly to keep working without having a quick break from study.

def raw_input(x):
  input(x)

Then when I run raw_input('Enter your first name: ') on the script I was working on, it captures it as does input() would.

There may be a reason not to do this, that I haven’t come across yet!