2017-02-21 118 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

我會,但那是我需要的部分,所以它不會每次都說「你好」。 –

相關問題