Python(if,elif,else)使用时间戳

Python (if, elif, else) working with timestamp

我正在学习一门关于Python编程的入门课程,我很难让下面的代码正常工作。分配要求:编写一个python代码,该代码使用"strftime()"函数获取今天的工作日值,然后使用"if..elif..else"语句显示关联的消息。所以,今天对我来说是星期五(W=5),它应该印上"预防胜于治疗"。相反,它不断印上"蠢与蠢"这句话。建议?

1
2
3
4
5
6
7
8
9
10
11
12
import datetime

t = datetime.date.today()
w = t.strftime("%w"); # day of week

if (w == 0): print("The devil looks after his own.");
elif (w == 1): print("Everything comes to him who waits.");
elif (w == 2): print("Give credit where credit is due.");
elif (w == 3): print("If you pay peanuts, you get monkeys.");
elif (w == 4): print("Money makes the world go round.");
elif (w == 5): print("Prevention is better than cure.");
else: print("Stupid is as stupid does.");


不使用返回strstrftime(),而是使用返回intweekday()

1
2
t = datetime.date.today()
w = today.weekday() + 1  # +1 because weekday() is 0-based, %w is 1-based

在python中,字符串和数字永远不相等。