[英]python 3 NameError
編輯 :謝謝您的幫助! 我下載了Python 3.7.0,但您是對的,我的Mac運行的是Python 2.7。 我現在有作業:)弄清楚如何使其運行3.7。 如果還有其他問題,我會回來。 謝謝!
初學者在這里。 在Mac中使用Python Launcher執行時出現NameError。 在Python 3.7.0 Shell中進行測試時,它可以正常運行。 我已經閱讀了NameError問題的其他答案,但不了解我在做什么錯。 感謝幫助。
使用的代碼
first_name = input ("Hi, what's your first name? ")
print ("Hi," , first_name)
收到錯誤
Traceback (most recent call last):
File "/Users/imperio/Documents/pythonpractice/Name.py", line 1, in <module>
first_name = input ("Hi, what's your first name? ")
File "<string>", line 1, in <module>
NameError: name 'Imperio' is not defined
這很可能是因為您沒有使用Python 3+執行它。
請檢查python -V
的輸出以查看執行代碼的版本。
在Mac上,您可能已經安裝了兩者,有時在python3 file.py
下為Python 3 python3 file.py
別名。
這是您的程序轉換為有效的Python2:
first_name = raw_input ("Hi, what's your first name? ")
print ("Hi, {}".format(first_name))
您正在運行Python2。在Python 2中, input
執行輸入。 raw_input
僅返回輸入的字符串。
這是一個例子:
>>> x = 1
>>> y = 2
>>> z = 3
>>> print input('variable? ')
variable? x # Note output is the value of the variable
1
>>> print input('variable? ')
variable? w # Variable 'w' doesn't exist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'w' is not defined
>>> print raw_input('variable? ') # Raw input just returns the input.
variable? x
x
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.