簡體   English   中英

Python(if,elif,else)使用時間戳

[英]Python (if, elif, else) working with timestamp

我正在學習Python入門編程課程,但無法獲取下面的代碼才能正常工作。 作業要求:編寫一個Python代碼,該代碼使用“ strftime()”函數獲取今天的工作日值,然后使用“ if..elif..else”語句顯示關聯的消息。 因此,對於我來說今天是星期五(w == 5),它應打印“預防勝於治療”。 相反,它將繼續打印else語句“愚蠢與愚蠢”。 忠告?

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

代替使用返回一個str strftime() ,而使用一個返回int weekday()

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

在Python中,字符串和數字從不相等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM