因此,爲了弄清十月十五號以來的任何一天的星期幾,你可以使用一個簡單的算術,我的問題是我從文件中讀取日期(例如2009-06 -12)和我已經把公式中:在Python 3.3中求解方程
w = (d + [2.6 * m - 0.2] + Y + [Y/4] + 5 * C + [C/4]) % 7
的日期的格式爲YYYY-MM-DD,我的代碼如下所示:
count = 5
f = open('/Users/student/Desktop/Harry.txt').readlines()[count]
Y = f[2:4]
C = f[:2]
m = f[5:7]
d = f[8:10]
w = (d + [2.6 * m - 0.2] + Y + [Y/4] + 5 * C + [C/4]) % 7
if w == 0:
print (f, "is a Sunday")
elif w == 1:
print (f, "is a Monday")
elif w == 2:
print (f, "is a Tuesday")
elif w == 3:
print (f, "is a Wednesday")
elif w == 4:
print (f, "is a Thursday")
elif w == 5:
print (f, "is a Friday")
elif w == 6:
print (f, "is a Saturday")
澄清:
w = day of the week counting from Sunday = 0 Monday = 1
d = the day of the month (for e.g. 28th 13th)
m = month number where March = 1 etc.
Y = last 2 digits of year
C = first 2 digits of year
但我得到這個錯誤
Traceback (most recent call last):
File "/Users/student/Documents/workspace/Tutorial Challenges/src/Day_Of_The_Week.py", line 7, in <module>
w = (d + [2.6 * m - 0.2] + Y + [Y/4] + 5 * C + [C/4]) % 7
TypeError: can't multiply sequence by non-int of type 'float'
幫助將不勝感激。
香港專業教育學院tryed是尚未它沒有讓我不能改變「名單」到「廉政」 – Obcure
添加額外的信息。 – user2357112
是的嘗試,以及仍然無法正常工作? – Obcure