更复杂的用户输入—扫描单词
惭愧,惭愧,断断续续地学习,致《笨方法学Python》才学到这里
参考: 习题—48
把书里提供的各类型单词弄成一个个列表元素,然后再按类型分成对应的元组,单词比较多的时候超级实用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # coding: utf-8 2 3 # 方向类的单词、动词、修饰词、名词 4 directions = "north south east west down up left right back" 5 verbs = "go stop kill eat" 6 stops = "the in of from at it" 7 nouns = "door bear princess cabinet" 8 ept = [] 9 10 # 获得各单词类型的元组列表 11 def GetList(variable): 12 13 # 把字符串按空格分成一个个列表元素 14 variable = variable.split() 15 16 for i in variable: 17 if "up" in variable: # 方向类 18 type = ("direction", i) 19 elif "go" in variable: # 动词类 20 type = ("verbs", i) 21 elif "in" in variable: # 修饰类 22 type = ("stops", i) 23 elif "door" in variable:# 名词类 24 type = ("nouns", i) 25 26 # 把各元组添加进空列表 27 ept.append(type) 28 29 GetList(directions) 30 GetList(verbs) 31 GetList(stops) 32 GetList(nouns)
当然,也可以和苦逼的我一样,直接写成元组列表的形式,这样比较整洁,但是费时
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 direction = [('direction', "north"), ('direction', 'south'), ('direction', 'east'), 2 ('direction', 'west'), ('direction', 'down'), ('direction', 'up'), 3 ('direction', 'left'), ('direction', 'right'), ('direction', 'back')] 4 nouns = [('noun', 'door'), ('noun', 'bear'), ('noun', 'princess'), ('noun', 'cabinet')] 5 stops = [('stop', 'the'), ('stop', 'in'), ('stop', 'of'), ('stop', 'from'), ('stop', 'at'), ('stop', 'it')] 6 verbs = [('verb', 'go'), ('verb', 'stop'), ('verb', 'kill'), ('verb', 'eat')] 7 8 word_list = [] 9 10 word_list.append(direction) 11 word_list.append(nouns) 12 word_list.append(stops) 13 word_list.append(verbs)
在这里我们根据第二种列表来写代码,在开始下一步时,先讲一下要注意的return语句,因为return只能在
def语句范围内使用, 在不使用条件判断语句的情况下,只能有一个return语句, 当两个return语句都成立时,
也只能返回第一个。看下面的示例就知道啦!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # coding: utf-8 2 3 def do(): 4 str = "Hello" 5 list = ["a", "b"] 6 return str 7 return list 8 9 def did(): 10 str = "Hello" 11 list = ["a", "b"] 12 return str, list 13 14 print do() 15 print did() 16 print did()[1]
这是扫描器的第二个部分的代码,因为前面的测试代码开头是导入 from ex48 import lexicon(从ex48.py
脚本里面导入lexicon) 且使用 lexicon.scan("go kill eat")这样的形式,证明 lexicon 是一个类的实例,而
scan是类里面定义的函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Lexicon(object): 2 3 def scan(self, enter): 4 # enter是我们输入的字符串,enter即输入 5 self.list = enter.split() 6 # ept是装单词元组用的,而single则是装载所有单词的列表,当i不存在于single时,则是错误的单词 7 ept = [] 8 single = [] 9 10 try: 11 # 当输入的字符串不是数字型时, int(enter)将会产生ValueError的异常/错误, 就会进入except语句 12 int(enter) 13 for num in self.list: 14 # 注意我们要的是如("numbers", 123)里面的int整数型的数字 15 ept.append(int(num)) 16 except ValueError: 17 # 通常输入的都是多个单词,所以用for语句提取每个单词,与从元组列表提取出来的单词进行比较 18 for i in self.list: 19 # li取list/列表的缩写,是像directions,nouns这样的元组列表 20 for li in word_list: 21 # tup取tuple/元组的缩写,是像("direction", "north"), ("direction", "south")这样的元组 22 for tup in li: 23 # word是"direction", "north", "direction", "south"这样的单词 24 for word in tup: 25 single.append(word) 26 if i == word: 27 # 这里不能写成return的形式,不然,只会返回含一个元组的列表 28 ept.append(tup) 29 30 if i not in single: 31 ept.append(("error", i)) 32 33 # 注意, 这里return的位置尤其重要, 当所有循环都执行完时, 才能返回总的列表 34 return ept 35 36 lexicon = Lexicon() 37 38 print lexicon.scan("bear I'm coming") 39 print lexicon.scan("I'm here")