簡體   English   中英

如何在 Python 3 中使用 raw_input?

[英]How do I use raw_input in Python 3?

在 Python 2 中:

raw_input()

在 Python 3 中,我收到一個錯誤:

NameError:未定義名稱“raw_input”

從 Python 3 開始, raw_input()被重命名為input()

來自Python 3.0 中的新增功能,內置部分第二項。

這適用於 Python 3.x 和 2.x:

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

解決此問題的可靠方法是

from six.moves import input

6是一個修補許多 2/3 常見代碼庫痛點的模塊。

在Python 3.xx中,您只需要input()而不是raw_input()

正如其他人所指出的那樣, raw_input函數在 Python 3.0 中已重命名為input ,並且您確實會得到一本更新的書更好的服務,但我想指出,有更好的方法來查看輸出你的腳本。

根據您的描述,我認為您使用的是 Windows,您已經保存了一個.py文件,然后您雙擊它來運行它。 程序結束后彈出的終端窗口會立即關閉,因此您看不到程序的結果。 為了解決這個問題,你的書建議添加一個raw_input / input語句來等到用戶按下回車鍵。 但是,正如您所看到的,如果出現問題,例如程序中的錯誤,該語句將不會被執行,並且窗口將關閉,而您無法看到出了什么問題。 您可能會發現使用命令提示符或 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

...然后按 Enter。 (如果您收到“python”不是可識別的命令的錯誤,請參閱http://showmedo.com/videotutorials/video?name=960000&fromSeriesID=96 )當您的程序運行完成時,無論是否成功完成,窗口將保持打開狀態,命令提示符將再次出現,供您鍵入另一個命令。 如果你想再次運行你的程序,你可以按向上箭頭來調用你之前輸入的命令,然后按回車再次運行它,而不必每次都輸入文件名。

使用空閑

IDLE 是一個簡單的程序編輯器,隨 Python 一起安裝。 除其他功能外,它還可以在窗口中運行您的程序。 右鍵單擊您的.py文件並選擇“在 IDLE 中編輯”。 當您的程序出現在編輯器中時,按 F5 或從“運行”菜單中選擇“運行模塊”。 您的程序將在程序結束后保持打開的窗口中運行,您可以在其中輸入 Python 命令以立即運行。

Timmerman 的解決方案在運行代碼時效果很好,但如果您不想在使用 pyflakes 或類似的 linter 時出現Undefined name錯誤,您可以使用以下代碼:

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

這是我放入腳本中的一段代碼,我不想在與 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 的存在並做出相應的行為。 最好保持安全。

可能不是最好的解決方案,但在我來這里之前,我只是在飛行中做這個以繼續工作,而不會從學習中快速休息。

def raw_input(x):
  input(x)

然后,當我在正在處理的腳本上運行raw_input('Enter your first name: ')時,它會像input()那樣捕獲它。

可能有理由不這樣做,我還沒有遇到過!

下面的那一個呢? 應該允許您在 Python2 和 Python3 中使用 raw_input 或 input 以及 Python2 的 raw_input 語義(也就是 Python3 輸入的語義)

# raw_input isn't defined in Python3.x, whereas input wasn't behaving like raw_input in Python 2.x
# this should make both input and raw_input work in Python 2.x/3.x like the raw_input from Python 2.x 
try: input = raw_input
except NameError: raw_input = input

通過創建一個新函數,您可以通過另一種方式做到這一點。

import platform
def str_input(str=''):
    py_version = platform.python_version() # fetch the python version currently in use
    if int(py_version[0]) == 2:
       return raw_input(str) # input string in python2
    if int(py_version[0]) == 3:
       return input(str) # input string in python3

如何使用:

str_input("Your Name: ")

當我想創建可以在 python2 和 python3 中運行的腳本和輸入時,我會使用它

raw_input()函數已從Python 3.x中刪除。 取輸入python2曾經有兩個函數,分別是input()和raw_input()

如果要在3.x中具有2.x的相同功能以獲取輸入參數,請遵循此Wiki鏈接https://en.wikibooks.org/wiki/Python_Programming/Input_and_Output

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM