for循环中嵌套多个if语句的问题
老师,您好,我在编写某段程序时出现一些问题难以搞懂,忘能指点
#!usr/bin/python # -*- coding: UTF-8 -*- # lexicon.py # 扫描器模块,用以判断用户输入的词汇类型 #用户输入 user_input = raw_input("input now!!\n>") words = user_input.split() #分割字符串并以列表形式返回 def scan(words): #词汇表 direction_list = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back'] verb_list = ['go', 'stop', 'kill', 'eat'] stops_list = ['the', 'in', 'of', 'from', 'at', 'it'] nouns_list = ['door', 'bear', 'princess', 'cabinet'] #用于存储扫描处理结果 results = [] #遍历列表每个元素 for word in words: #判断是否为方向 if word in direcition_list: stuff = ('direction', word) return results.append(stuff) #判断是否为动词 if word in verb_list: stuff = ('verb', word) return results.append(stuff) #判断是否为修饰词 if word in stops_list: stuff = ('stops', word) return results.append(stuff) #判断是否为名词 if word in nouns_list: stuff = ('nouns', word) return results.append(stuff) #判断是否为数字 if convert_number(word): return results.append(convert_number(word)) def convert_number(s): try: return int(s) except ValueError: return None
File "lexicon.py", line 32 if word in verb_list: ^ SyntaxError: invalid syntax 1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # test.py 4 lists = [1,2,3,4,5,6,7,8] 5 list1 = [1,2] 6 list2 = [3,4,5] 7 list3 = [6,7,8] 8 for i in lists: 9 if i in list1: 10 print "1" 11 if i in list2: 12 print "2" 13 if i in list3: 14 print "3" 15 # python test.py 1 1 2 2 2 3 3
3
如上,因为第一段代码执行报错,所以我写了下面一段用来测试,发现for循环中嵌套使用多个if语句语法正确
我不知道是什么原因了