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

时间:2014-03-07 18:50:12

标签: python timestamp strftime

我正在开始使用Python编程类,但我无法使下面的代码正常工作。该作业要求:编写一个使用“strftime()”函数的Python代码来获取今天的工作日值,然后使用“if..elif..else”语句来显示相关的消息。所以,今天是星期五(w == 5)对我来说,应该打印出“预防胜于治疗”。相反,它继续打印else语句“Stupid就像愚蠢一样”。建议?

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.");

1 个答案:

答案 0 :(得分:0)

使用strftime(),而不是使用返回str的{​​{1}},而是返回weekday()

int

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