[英]input() error - NameError: name '...' is not defined
當我嘗試運行這個簡單的腳本時出現錯誤:
input_variable = input("Enter your name: ")
print("your name is" + input_variable)
假設我輸入了“dude”,我得到的錯誤是:
line 1, in <module>
input_variable = input("Enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'dude' is not defined
我正在運行 Mac OS X 10.9.1,我正在使用 Python 3.3 安裝附帶的 Python Launcher 應用程序來運行腳本。
TL; 博士
Python 2.7 中的input
函數,將您輸入的任何內容作為 Python 表達式進行評估。 如果你只是想讀取字符串,那么在 Python 2.7 中使用raw_input
函數,它不會評估讀取的字符串。
如果您使用的是 Python 3.x, raw_input
已重命名為input
。 引用Python 3.0 發行說明,
raw_input()
被重命名為input()
。 也就是說,新的input()
函數從sys.stdin
讀取一行並返回它,並去掉尾部的換行符。 如果輸入過早終止,則會引發EOFError
。 要獲得input()
的舊行為,請使用eval(input())
在 Python 2.7 中,有兩個函數可用於接受用戶輸入。 一個是input
,另一個是raw_input
。 你可以認為它們之間的關系如下
input = eval(raw_input)
考慮以下代碼以更好地理解這一點
>>> dude = "thefourtheye"
>>> input_variable = input("Enter your name: ")
Enter your name: dude
>>> input_variable
'thefourtheye'
input
接受來自用戶的字符串並評估當前 Python 上下文中的字符串。 當我輸入dude
作為輸入時,它發現dude
綁定到值thefourtheye
,因此評估結果變為thefourtheye
並分配給input_variable
。
如果我輸入當前 python 上下文中不存在的其他內容,它將失敗NameError
。
>>> input("Enter your name: ")
Enter your name: dummy
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'dummy' is not defined
Python 2.7 input
安全注意事項:
由於評估了任何用戶類型,它也會帶來安全問題。 例如,如果您已經在程序中使用import os
加載了os
模塊,然后用戶輸入
os.remove("/etc/hosts")
這將被python評估為函數調用表達式,並將被執行。 如果您使用提升的權限執行 Python, /etc/hosts
文件將被刪除。 看,這有多危險?
為了演示這一點,讓我們再次嘗試執行input
函數。
>>> dude = "thefourtheye"
>>> input("Enter your name: ")
Enter your name: input("Enter your name again: ")
Enter your name again: dude
現在,當input("Enter your name: ")
被執行時,它等待用戶輸入並且用戶輸入是一個有效的 Python 函數調用,所以它也會被調用。 這就是為什么我們會看到Enter your name again:
再次提示。
所以,你最好使用raw_input
函數,就像這樣
input_variable = raw_input("Enter your name: ")
如果需要將結果轉換為其他類型,則可以使用適當的函數來轉換raw_input
返回的字符串。 例如,要將輸入讀取為整數,請使用int
函數,如本答案所示。
在 python 3.x 中,只有一個函數來獲取用戶輸入,即input
,它相當於 Python 2.7 的raw_input
。
您正在運行 Python 2,而不是 Python 3。為了在 Python 2 中工作,請使用raw_input
。
input_variable = raw_input ("Enter your name: ")
print ("your name is" + input_variable)
由於您正在為 Python 3.x 編寫代碼,因此您需要以以下內容開始您的腳本:
#!/usr/bin/env python3
如果您使用:
#!/usr/bin/env python
它將默認為 Python 2.x。 如果沒有以#! (又名shebang)。
如果您的腳本只是從以下內容開始:
#! python
然后您可以將其更改為:
#! python3
雖然這種較短的格式只能被少數程序識別,例如啟動器,因此它不是最佳選擇。
前兩個示例使用更廣泛,有助於確保您的代碼可以在任何安裝了 Python 的機器上運行。
我也遇到了一個應該與 python 2.7 和 3.7 兼容的模塊的問題
我發現解決問題的方法是導入:
from six.moves import input
這修復了兩個解釋器的可用性
您可以在此處閱讀有關六個圖書館的更多信息
您應該使用raw_input
因為您使用的是 python-2.7。 當您在變量上使用input()
時(例如: s = input('Name: ')
),它將在 Python 環境中執行命令而不保存您在變量 ( s
) 上寫的內容,並在以下情況下創建錯誤你寫的沒有定義。
raw_input()
將正確保存您在變量上寫的內容(例如: f = raw_input('Name : ')
),並且不會在 Python 環境中執行它而不會產生任何可能的錯誤:
input_variable = raw_input('Enter Your Name : ')
print("Your Name Is : " + (input_variable))
對於python 3及以上
s = raw_input()
如果您正在在線網站上解決hackerrank,它將解決pycharm IDE上的問題,然后使用:
s = input()
input_variable = input ("Enter your name: ")
print ("your name is" + input_variable)
您必須以單引號或雙引號輸入
Ex:'dude' -> correct
dude -> not correct
我們正在使用以下適用於python 2 和 python 3
#Works in Python 2 and 3:
try: input = raw_input
except NameError: pass
print(input("Enter your name: "))
對於可能遇到此問題的任何其他人,即使您在腳本的開頭包含#!/usr/bin/env python3
,如果文件不可執行,shebang 也會被忽略。
要確定您的文件是否可執行:
./filename.py
-bash: ./filename.py: Permission denied
,運行chmod a+x filename.py
./filename.py
如果你已經包含了import sys; print(sys.version)
import sys; print(sys.version)
正如凱文建議的那樣,你現在會看到腳本正在被python3解釋
你可以這樣做:
x = raw_input("enter your name")
print "your name is %s " % x
或者:
x = str(input("enter your name"))
print "your name is %s" % x
之前的貢獻不錯。
import sys; print(sys.version)
def ingreso(nombre):
print('Hi ', nombre, type(nombre))
def bienvenida(nombre):
print("Hi "+nombre+", bye ")
nombre = raw_input("Enter your name: ")
ingreso(nombre)
bienvenida(nombre)
#Works in Python 2 and 3:
try: input = raw_input
except NameError: pass
print(input("Your name: "))
Enter your name: Joe ('Hi ', 'Joe', <type 'str'>) Hi Joe, bye Your name: Joe Joe
謝謝!
有兩種方法可以解決這些問題,
第一個很簡單,無需更改代碼
通過 Python3 運行您的腳本,
如果你仍然想在 python2 上運行,那么在運行你的 python 腳本之后,當你輸入輸入時請記住
string
那么只需開始輸入“輸入帶有雙引號”,它就可以在 python2.7 和第二種方式是更改代碼
使用以下導入並與任何版本的 python 一起運行
from six.moves import input
raw_input()
函數而不是input()
函數str()
函數(如str(input())
清理您的代碼,然后分配給任何變量正如錯誤所暗示的那樣:
名稱 'dude' 未定義,即對於 python 'dude' 在這里變為變量,並且它沒有分配任何 python 定義類型的值
所以只有它像嬰兒一樣哭泣所以如果我們定義一個“dude”變量並分配任何值並傳遞給它,它會起作用,但這不是我們想要的,因為我們不知道用戶將輸入什么,而且我們想要捕獲用戶輸入。
關於這些方法的事實:
input()
函數:該函數采用您輸入的輸入的值和類型,而不修改它的類型。
raw_input()
函數:該函數將您提供的輸入顯式轉換為字符串類型,筆記:
input() 方法的漏洞在於,任何人都可以通過使用變量名或方法名來訪問訪問輸入值的變量。
您可以更改您在 IDE 中使用的 python,如果您已經下載了 python 3.x,切換應該不會太難。 但是你的腳本在 python 3.x 上運行良好,我只想改變
print ("your name is" + input_variable)
到
print ("your name is", input_variable)
因為使用逗號,它會在your name is
和用戶輸入的任何內容之間打印一個空格。 並且:如果您使用的是 2.7,只需使用raw_input
而不是 input。
這是一個與 Python 2.7 和 Python 3+ 兼容的輸入函數:(@Hardian 稍微修改了答案)以避免UnboundLocalError: local variable 'input' referenced before assignment
錯誤UnboundLocalError: local variable 'input' referenced before assignment
def input_compatible(prompt=None):
try:
input_func = raw_input
except NameError:
input_func = input
return input_func(prompt)
這里還有另一種沒有try
塊的替代方法:
def input_compatible(prompt=None):
input_func = raw_input if "raw_input" in __builtins__.__dict__ else input
return input_func(prompt)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.