2017-02-21 116 views
0

我有一个这个代码的问题,因为我一直在创建一个语法错误。我用不同的问候语创建了一个列表,但我无法在第24行中找到它。请帮助。谢谢:)不支持的操作数类型为+:'NoneType'和'str'tkinter错误

import pyttsx 
import sklearn 
import random 

speech_engine = pyttsx.init('sapi5') # see  http://pyttsx.readthedocs.org/en/latest/engine.html#pyttsx.init 
speech_engine.setProperty('rate', 150) 
def Hello(): 

    words = [line.strip() for line in open('Hello.txt')] 
    speak(random.choice(words)) 




def speak(text): 
    speech_engine.say(text) 
speech_engine.runAndWait() 


intro=(Hello()) 

Greeting=input(speak("What is your name")) 

Account=input(speak(intro + Greeting + ", Would you like to Log in or create an account")) 


if Account==("Create An Account") or Account==("Create An Account") or Account==("create an account"): 
     Password=input(speak("What is the password going to be for your account")) 
     text_file = open("Password.txt", "a") 
     text_file.write("|   "+ Greeting +"     |   "+ Password +"   |\n") 
     text_file.close() 

回答

2

因为你的函数Hello()return什么,它含蓄地返回None,所以介绍=无。现在,您尝试向“无”添加一个字符串,这正是您的错误信息所指出的内容。

如果您只是想打电话给您的Hello()函数来迎接用户,只需通过调用Hello()而不指定返回值即可。无论如何,因为它是None,所以没有明显的理由将它包含在input(...)声明中。

编辑:

考虑您的意见考虑在内,我建议你改变你的函数:

def Hello(): 
    words = [line.strip() for line in open('Hello.txt')] 
    return random.choice(words) 
+0

是有办法解决这一问题?对不起,我对此很陌生 –

+0

取决于你用'intro + Greeting +“......”'来实现的目标。现在删除'intro +'部分可以解决这个特定的问题。 –

+0

我会,但那是我需要的部分,所以它不会每次都说“你好”。 –

相关问题