我試圖看到每個可能的解決方案,但由於某種原因,他們都沒有工作。我做學習Python的硬盤的方式鍛鍊49.我做我自己的解析器和它的作品,當我通過pycharm運行它,當我打開控制檯或PowerShell和嘗試導入,它給出了這樣的錯誤:Pycharm不斷給名稱'類'沒有定義錯誤
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'parse_sentence' is not defined
我也嘗試看看問題是否在腳本中,所以我將他的工作複製到另一個選項卡上,它給了我同樣的問題。
這裏我的代碼一點點:
class Sentence:
def __init__(self, type):
self.type = type
def subjects(self):
nouns = ('door', 'bear', 'princess', 'cabinet')
s = self.scan(self.type)
for i in s:
for k in i:
if k in nouns:
print k
def verbs(self):
verbs = ('go', 'stop', 'kill', 'eat', 'open')
s = self.scan(self.type)
for i in s:
for k in i:
if k in verbs:
print k
def objects(self):
directions = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back')
nouns = ('door', 'bear', 'princess', 'cabinet')
s = self.scan(self.type)
for i in s:
for k in i:
if k in directions or k in nouns:
print k
def get_tuple(self, word):
directions = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back')
verbs = ('go', 'stop', 'kill', 'eat', 'open')
stop_words = ('the', 'in', 'of', 'from', 'at', 'it')
nouns = ('door', 'bear', 'princess', 'cabinet')
lowercased = word.lower()
if lowercased in directions:
return ('direction', lowercased)
elif lowercased in verbs:
return ('verb', lowercased)
elif lowercased in stop_words:
return ('stop_words', lowercased)
elif lowercased in nouns:
return ('noun', lowercased)
elif lowercased.isdigit():
return ('number', int(lowercased))
else:
return ('error', word)
def scan(self, sentence):
words = sentence.split()
return [self.get_tuple(word) for word in words]
是不是因爲self.scan
的,它是在課堂上,我只是不希望發佈所有的代碼不亂的頁面了。我打開控制檯,我輸入import parser
(解析器就是這個文件的名字),然後我輸入myarg = Sentence('let us kill the bear')
。給了我上面的錯誤。 Samehting,當我走他的路時,非常感謝你提前閱讀此內容。
我在Windows 10,Python 2.7版
這裏是我的錯誤
import parser
myarg = Sentence('let us kill the bear')
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'Sentence' is not defined
做 進口PASER時 parser.Sentence()
我得到:
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Sentence'
在做什麼時:
import parser
myarg = parser.Sentence('let us kill the bear')
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Sentence'
我將添加代碼的其餘部分,如果有人需要它 – Omar
總是有問題顯示完整的錯誤信息(追蹤)還有其他有用的信息 - 即。哪一行出問題。我在代碼中看不到'parse_sentence'。 – furas
你好,謝謝你的回覆。我aplogoize我忘了複製我的錯誤。這是我嘗試做Zed Shaw的解析器例子時遇到的錯誤。這裏是全碼: – Omar