2014-09-23 60 views
1

我一直得到这个错误的像20“if ntext [x] in dictionary:”。该程序将文字说话转换成英语。TypeError:'NoneType'类型的参数是不可迭代的python

import csv 
def CreateDictionary(): 
    fo = open("textToEnglish2014.csv" , "r") 
    dictonary = {} 
    reader = csv.reader(fo) 
    for row in reader: 
     dictionary[row[0]] = row[1] 
     return dictionary 

def main(): 
    dictionary = CreateDictionary() 
    y = "y" 
    while y == "y": 
     text = input("Enter text to which you would like conversion: ") 
     text = text.lower() 
     ntext = text.split(" ") 
     new_text = "" 
     x = 0 
     while x < len(ntext): 
      if ntext[x] in dictionary: 
       new_text = new_text + dictionary[ntext[x]] + " " 
      else: 
       export = export + "NF " 
      x += 1 
     print (new_text) 
     y = input("Continue conversion? y or q ") 

main() 
+3

是在CreateDictionary()意味着要缩进这样的 “回报”?如果是这样,CreateDictionary将在第一行之后返回。 – 2014-09-23 01:36:04

+0

如果有第一行,它将只返回字典;如果没有,则会从函数的末尾落下并返回'None'。 – user2357112 2014-09-23 01:59:17

回答

2
dictonary = {} 

应该

dictionary = {} 

注意拼写

相关问题