NameError: name “ ” is not defined


NameError: name “ ” is not defined

問題一:name ‘name’ is not defined

"name"兩端是雙下划線"_",不是只有一個""。

問題二:name 'messagebox' is not defined

“ ” 內為某個數據庫的子module。

在代碼中加上如下語句:

from tkinter import messagebox

默認情況下子module不會自動import。

問題三:name 'reload' is not defined.

對於 Python 2.X
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
對於 Python 3.3
import imp
imp.reload(sys)

注意:

  1. Python 3 與 Python 2 有很大的區別,其中Python 3 系統默認使用的就是utf-8編碼。

  2. 所以,對於使用的是Python 3 的情況,就不需要sys.setdefaultencoding("utf-8")這段代碼。

  3. 最重要的是Python 3 的 sys 庫里面已經沒有 setdefaultencoding() 函數了。

    對於 Python 3.4:
import importlib
importlib.reload(sys)

問題四:name 'file' is not defined

  f = file('poem.txt', 'w') # open for 'w'riting
NameError: name 'file' is not defined

解決辦法:file()改為open()

問題五:name 'array' is not defined

區分array和list,弄清楚道題想要啥類型;加載array模塊。

from array import array

問題六: name 'xx' is not defined

IndentationError:expected an indented block

問題在於tab和空格混用導致出現了問題。

問題七:name 'math' is not defined

from math import *

改為

import math

問題八:python2中input出現的name“ ” is notdefined.

Python 2.X中對於input函數來說,它所希望讀取到的是一個合法的Python表達式,即你在輸入字符串的時候必須要用""將其擴起來;而在Python 3中,input默認接受的是str類型。

解決辦法:1、在控制台進行輸入參數時,將其變為一個合法的Python表達式,用" "將其擴起來

2、使用raw_input,因為raw_input將所有的輸入看作字符串,並且返回一個字符串類型。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM