我一直在尝试创建一个简单的程序,它将“登录”用户并欢迎他们。我对编程很陌生,因此对Python抛出的错误感到非常困惑。我也知道在这个特殊的错误上有很多问题得到了回答,但是没有一个问题和我遇到的问题完全一致,而且因为我是新来的,所以我不能适应这些答案。☺
这是我的代码。。。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都会这样说-Please type your Username> User1
The time is 19:09
Traceback (most recent call last):
File "/home/pi/Documents/User.py", line 15, in
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错误。我的问题是,为什么会这样?非常感谢所有回答我这个简单问题的人☺
干杯,卡尔