不支持的操作数类型+:'NoneType'和'str'tkinter错误

时间:2017-02-21 15:48:08

标签: python string tkinter python-3.5 nonetype

我在使用此代码时遇到问题,因为我一直存在语法错误。我创建了一个带有不同问候的列表,但是我无法在24号线上回忆起来。请帮忙。谢谢:))

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()

1 个答案:

答案 0 :(得分:2)

由于您的函数Hello()没有return任何内容,因此它会隐式返回None,因此intro = None。现在你试着"添加"字符串为None,这正是您的错误消息所声明的内容。

如果您只是想调用Hello()函数来问候用户,只需调用Hello()而不指定返回值即可。因为它是None,所以没有明显的理由将其包含在input(...)语句中。

编辑:

考虑到您的评论,我建议您更改功能:

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