在Python 2中:
raw_input()
在Python 3中,我得到一个错误:
name 'raw_input'没有定义
在Python 2中:
raw_input()
在Python 3中,我得到一个错误:
name 'raw_input'没有定义
当前回答
正如其他人所指出的,raw_input函数在Python 3.0中已被重命名为input,您真的应该读一本最新的书籍,但我想指出的是,还有更好的方法来查看脚本的输出。
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.
使用命令提示符
当您查看包含Python程序的文件夹窗口时,按住shift并右键单击窗口白色背景区域的任何位置。弹出的菜单应该包含一个条目“在这里打开命令窗口”。(我认为这适用于Windows Vista和Windows 7。)这将打开一个命令提示窗口,看起来像这样:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Weeble\My Python Program>_
要运行您的程序,输入以下内容(替换您的脚本名称):
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.
使用闲置
IDLE是一个简单的程序编辑器,随Python一起安装。在其他功能中,它可以在窗口中运行程序。右键单击你的.py文件,选择“在IDLE中编辑”。当程序出现在编辑器中时,按F5或从“运行”菜单中选择“运行模块”。程序将在程序结束后保持打开的窗口中运行,您可以在其中输入Python命令立即运行。
其他回答
正如其他人所指出的,raw_input函数在Python 3.0中已被重命名为input,您真的应该读一本最新的书籍,但我想指出的是,还有更好的方法来查看脚本的输出。
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.
使用命令提示符
当您查看包含Python程序的文件夹窗口时,按住shift并右键单击窗口白色背景区域的任何位置。弹出的菜单应该包含一个条目“在这里打开命令窗口”。(我认为这适用于Windows Vista和Windows 7。)这将打开一个命令提示窗口,看起来像这样:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Weeble\My Python Program>_
要运行您的程序,输入以下内容(替换您的脚本名称):
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.
使用闲置
IDLE是一个简单的程序编辑器,随Python一起安装。在其他功能中,它可以在窗口中运行程序。右键单击你的.py文件,选择“在IDLE中编辑”。当程序出现在编辑器中时,按F5或从“运行”菜单中选择“运行模块”。程序将在程序结束后保持打开的窗口中运行,您可以在其中输入Python命令立即运行。
这在python3中有效。X和2.x:
# Fix Python 2.x.
try: input = raw_input
except NameError: pass
print("Hi " + input("Say something: "))
下面是我放在脚本中的一段代码,我不想在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是否存在并进行相应的操作。还是小心为妙。
Timmerman的解决方案在运行代码时工作得很好,但如果你不想在使用pyflakes或类似的linter时得到未定义的名称错误,你可以使用以下方法:
try:
import __builtin__
input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
pass
可能不是最好的解决方案,但在我来这里之前,我只是在飞行中继续工作,没有从学习中快速休息。
def raw_input(x):
input(x)
然后,当我在我正在处理的脚本上运行raw_input('输入您的第一个名字:')时,它会像input()一样捕获它。
也许有一个理由不这样做,我还没有遇到!