无法将“nonetype"对象隐式转换为 str - Python3 [英] Can't convert 'nonetype' object to str implicitly - Python3

查看:71
本文介绍了无法将“nonetype"对象隐式转换为 str - Python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个简单的程序,该程序将登录"用户并欢迎他们.我对编程很陌生,因此我对 Python 抛出的错误感到非常困惑.我也知道关于这个特定错误已经回答了很多问题,但是它们似乎都与我遇到的问题不完全一致,而且因为我是新手,我无法调整答案.☺

I've been trying to create a simple program which will 'log' a user in and welcome them. I'm very new to programming and therefore am absolutely baffled by the errors Python is throwing up. I am also aware that there have been many questions answered on this particular error, however none of them seem to coincide exactly with the problem I am getting, and because I'm new I can't adapt the answers. ☺

这是我的代码...

    from datetime import datetime

    def userLogin() :
        Name = input("Please type         your Username ")
        if Name == "User1" :
            print ("Welcome User1!         " + timeIs())
        if Name == "User2" :
            print ("Welcome User2! The time is " +         datetime.strftime(datetime.now(), '%H:%M'))
                if Name == "User3" :
                    print ("Welcome User3! The time is " + datetime.strftime(datetime.now(), '%H:%M'))

    def timeIs() :
        print ("The time is " +   datetime.strftime(datetime.now(), '%H:%M'))

    print (userLogin())

如您所见,对于 User2 和 User3,我已经列出了通过 datetime 模块获取时间的完整操作.然而,在 User1 语句中,我试图通过定义第二个语句 (timeIs) 并使用它来说明时间来缩短它.每次我登录"user1 时,python 都会说这个-

As you can see, for User2 and User3 I have set out the full operation for getting the time via the datetime module. In the User1 statement however I have tried to shorten it down by defining a second statement (timeIs) and using that to state the time. Every time I 'log in' user1, python says this-

    Please type your Username> User1
    The time is 19:09
    Traceback (most recent call last):
      File "/home/pi/Documents/User.py", line 15, in <module>
print (userLogin())
   File "/home/pi/Documents/User.py", line 6, in userLogin
print ("Welcome User1! " + timeIs())
    TypeError: Can't convert 'NoneType' object to str implicity

如你所见,python接受输入,吐出没有欢迎信息的时间,并给出nonetype错误的东西.我的问题是,为什么会发生这种情况?非常感谢所有回答我非常简单的问题的人☺

As you can see, python takes the input, spits out the time without the welcome message, and gives the nonetype error thing. My question is, why is this happening? Thank you very much to everyone who answers my very simple question ☺

干杯,卡尔

推荐答案

函数返回一个值.如果您没有指定,它们会隐式返回 None.你的 timeIs 函数打印了一些东西,但没有 return 语句,所以它返回 None.您可以保持原样并以不同的方式调用它:

Functions return a value. If you don't specify one, they implicitly return None. Your timeIs function prints something, but doesn't have a return statement, so it returns None. You can leave it as-is and call it differently:

if Name == "User1" :
    print("Welcome User1!         ", end='')
    timeIs()

或者你可以用同样的方式调用它但定义不同,返回创建的字符串而不是打印它:

Or you can call it in the same way but define it differently, returning the created string instead of printing it:

def timeIs() :
    return "The time is " +   datetime.strftime(datetime.now(), '%H:%M')

这篇关于无法将“nonetype"对象隐式转换为 str - Python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆