无法将“nonetype”对象隐式转换为 str - Python3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34715434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't convert 'nonetype' object to str implicitly - Python3
提问by Carl
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. ?
我一直在尝试创建一个简单的程序,它将“登录”用户并欢迎他们。我对编程很陌生,因此我对 Python 抛出的错误感到非常困惑。我也知道关于这个特定错误已经回答了很多问题,但是它们似乎都与我遇到的问题不完全一致,而且因为我是新手,我无法调整答案。?
Here is my code...
这是我的代码...
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())
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-
如您所见,对于 User2 和 User3,我已经列出了通过 datetime 模块获取时间的完整操作。然而,在 User1 语句中,我试图通过定义第二个语句 (timeIs) 并使用它来说明时间来缩短它。每次我“登录”user1 时,python 都会说-
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
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 ?
如您所见,python 接受输入,在没有欢迎消息的情况下吐出时间,并给出 nonetype 错误。我的问题是,为什么会发生这种情况?非常感谢所有回答我非常简单的问题的人?
Cheers, Carl
干杯,卡尔
采纳答案by TigerhawkT3
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:
函数返回一个值。如果您不指定一个,它们会隐式返回None
。你的timeIs
函数打印了一些东西,但没有return
声明,所以它返回None
. 您可以保持原样并以不同的方式调用它:
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')