将日期转换为字符串导致字符串中出现“None”,但在 python 中没有错误

Conversion of date to string resulting in "None" in the string but no error in python

提问人:Sabrina 提问时间:6/9/2023 最后编辑:LeGECSabrina 更新时间:6/9/2023 访问量:38

问:

我正在尝试创建一个电子邮件正文,然后我将能够发送,代码可以工作并返回我期望的值,直到:

email_text = "Please can you " + str(move_plates) + " and " + str(read_plates)

当我使用时:print(email_text)

然后我得到“请你无和无”但没有错误,任何人都可以建议如何解决这个问题吗?

当我将日期转换为字符串时,我假设这是一个问题。

def move_Plates():
    if _days_[movePlates.weekday()] == "Saturday":
        return ("change day " + str(_days_[movePlates.weekday()]) + "New Date: " + str(movePlates + timedelta(days = 2)))
    elif _days_[movePlates.weekday()] == "Sunday":
        return ("change day " + str(_days_[movePlates.weekday()]) + "New Date: " + str(movePlates + timedelta(days = 1)))
    else: 
        return (" move plates on: " + str(_days_[movePlates.weekday()]) + " " + str(movePlates))
move_plates = print(move_Plates())
move_plates

def read_Plates():
    if _days_[readPlates.weekday()] == "Saturday":
        return ("change day " + str(_days_[readPlates.weekday()])+ "New Date: " + str(movePlates + timedelta(days = 2)))
    elif _days_[readPlates.weekday()] ==  "Sunday":
        return ("change day " + str(_days_[readPlates.weekday()])+ "New Date: " + str(movePlates + timedelta(days = 1)))
    else: 
        return (" read plates on: " + str(_days_[readPlates.weekday()]) + " " + str(readPlates))
read_plates = print(read_Plates())
read_plates

email_text = "Please can you " + str(move_plates) + " and " + str(read_plates)
print(email_text)
python 字符串 date 日期

评论


答:

2赞 wolfrevo 6/9/2023 #1

该函数返回以下更改printNone

#move_plates = print(move_Plates())
...
#read_plates = print(read_Plates())

对此

move_plates = move_Plates()
...
read_plates = read_Plates()

评论

0赞 Sabrina 6/9/2023
那么,当我删除 print(move_Plates) 时,我将如何打印我的函数的结果,我在 0x000002EA25A2B7E0> 而不是结果上打印<函数move_Plates
2赞 LeGEC 6/9/2023 #2

要将函数的结果分配给变量,您应该编写:

move_plates = move_Plates()

不:

move_plates = print(move_Plates())

print(...)具有将文本打印到终端的效果(更准确地说是 stdout),但不返回任何内容。

如果你写 ,将永远是 。foo = print([anything])fooNone

评论

0赞 Sabrina 6/9/2023
那么,当我删除 print(move_Plates) 时,我将如何打印我的函数的结果,我在 0x000002EA25A2B7E0> 而不是结果上打印<函数move_Plates
0赞 LeGEC 6/9/2023
保留 . (带括号)将存储您在执行 Foo 时获得的结果,将存储函数本身(并且不会执行它)。()foo = Foo()foo = Foo