【问题标题】:unsupported operand type(s) for +: 'NoneType' and 'str' tkinter error+ 不支持的操作数类型:“NoneType”和“str”tkinter 错误
【发布时间】:2017-07-11 08:17:42
【问题描述】:

此代码有问题,因为我不断收到语法错误。我创建了一个包含不同问候语的列表,但我无法在第 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()

【问题讨论】:

    标签: python string tkinter python-3.5 nonetype


    【解决方案1】:

    由于您的函数 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)
    

    【讨论】:

    • 有没有办法解决这个问题?抱歉,我对此很陌生
    • 取决于您尝试使用intro + Greeting + "..." 实现的目标。现在删除 intro + 部分可以解决该特定问题。
    • 我会,但这是我需要的部分,所以它不会每次都说“你好”。
    • 现在你会在每次调用Hello() 函数时“说出”一个随机的问候语句。然后您尝试说出帐户创建问题。我认为最好让Hello() 函数返回一个随机问候语,然后可以在下面的speak() 函数中使用。
    猜你喜欢
    • 2014-06-15
    • 2018-07-07
    • 2023-01-04
    • 2015-09-21
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    相关资源
    最近更新 更多