今天写了一道python字典题目,用了上次字典(2)中的方法,代码如下:
1 json = {'totalCount':'1', 'ID':'1029', 'IP':'10.0.0.1'} 2 3 def find_value(themap, word): 4 if word in themap: 5 return themap[word] 6 else: 7 return "Not found." 8 9 json['_find'] = find_value 10 11 while True: 12 print "resule", 13 word = raw_input("> ") 14 if not word: break 15 16 number_found = json['_find'](json,word) 17 print number_found
写完怎么运行都不对,都是显示如下:
就是不出结果,只会一直输入。
错误原因:最后两句应该在循环里面,因为 每输入一次都运行def函数在列表中找对应的number。