笨方法學python 習題48 無法通過測試。。。如何解決?

時間 2021-06-02 23:40:39

1樓:xuqian

def scan(content):

stuff=

result=

words=content.split( )for word in words:

if word.isdigit():

word=int(word)

else:

return result

2樓:xuafaqing

# coding:utf-8

# 定義類Lexicon,關鍵字組合為字典

class Lexicon(object):

def __init__(self):

self.vocabulary = {'north': 'direction', 'south': 'direction', 'east': 'direction',

39;west': 'direction', 'down': 'direction', 'up': 'direction',

39;left': 'direction', 'right': 'direction', 'back': 'direction',

39;front': 'direction', 'go': 'verb', 'stop': 'verb',

39;kill': 'verb', 'eat': 'verb', 'the': 'stop',

39;in': 'stop', 'of': 'stop', 'from': 'stop',

39;at': 'stop', 'it': 'stop', 'door': 'noun',

39;bear': 'noun', 'princess': 'noun', 'cabinet': 'noun'

self.list =

# 建立掃瞄器

def scan(self, sentence):

self.list =

self.sentence = sentence

self.words = self.sentence.split()

for word in self.words:

if word.isdigit(): # 檢查是否為數字

try:

except:

else: # 檢查是否為關鍵字

try:

except:

return self.list

lexicon = Lexicon() # 例項化類Lexicon

我的測試也全部通過,呵呵;

3樓:

defconvert_number(s

):try

:return

int(s)

except

ValueError

:return

None

directions=(

'north'

,'south'

,'east'

,'west'

,'down'

,'up'

,'left'

,'right'

,'back'

)verbs=(

'go'

,'stop'

,'kill'

,'eat'

)stops=(

'the'

,'in'

,'of'

,'from'

,'at'

,'it'

)nouns=(

'door'

,'bear'

,'princess'

,'cabinet'

)def

scan

(stuff

):result=

words

=stuff

.split

()foriin

words:if

iindirections

:direct=(

'direction',i

)result.(

direct

)elifiin

verbs

:verb=(

'verb',i

)result.(

verb

)elifiin

stops

:stop=(

'stop',i

)result.(

stop

)elifiin

nouns

:noun=(

'noun',i

)result.(

noun

)elif

convert_number(i

):number=(

'number'

,convert_number(i

))result.(

number

)else

:error=(

'error',i

)result.(

error

)return

result

在學習《笨方法學Python》第25題時遇到了問題?

Tiger 本人用的python 3.6.2,使用的notepad 做為編譯環境 第一步 設定你的python的工作路徑,我的是 C Users Administrator Desktop python聯絡題 第二步 開啟cmd cd C Users Administrator Desktop py...

無基礎有啥好方法學python?

Richie 你清楚自己學python的目的是什麼?找工作?Raspberry Pi程式設計?機器學習?資料分析?Web開發?我學習python的過程幾乎是專案和興趣驅動的,大學的時候感興趣,給Raspberry Pi寫了個小程式,甚至都沒有用到物件導向.看著Raspberry Pi的教程順便學的p...

Python中列表的方法有什麼?

黑馬程式設計師教程 用於在列表末尾新增新的元素 copy 用於複製列表 count 用於統計某個元素在列表中出現的次數 extend 用於在列表末尾一次性新增另乙個序列中的所有元素,即使用新列表擴充套件原來的列表 index 用於從列表中找出某個值第乙個匹配項的索引位置insert 用於按照索引將元...