这是我的第四个 python 脚本,所以请耐心等待我的新手......我正在编写一个脚本来告诉某个日期的星期几。除了一个错误,一切都运行良好。我对出了什么问题有一个模糊的想法,但不太确定:
TypeError:“int”对象不可下标
#!/usr/bin/python
import sys, string
# Call example: day_of_week(2,10,1988); February 10, 1988
def day_of_week(month, date, year):
# January
if month == 1: m = 11
# February
elif month == 2: m = 12
# March
elif month == 3: m = 1
# April
elif month == 4: m = 2
# May
elif month == 5: m = 3
# June
elif month == 6: m = 4
# July
elif month == 7: m = 5
# August
elif month == 8: m = 6
# September
elif month == 9: m = 7
# October
elif month == 10: m = 8
# November
elif month == 11: m = 9
# December
elif month == 12: m = 10
# Calculate the day of the week
dow = (date + ((13 * m-1)/5) + year[2:] + (year[2:]/4) + (year[:2]/4) - 2 * year[:2]) % 7)
# Formatting!
if dow == 0: return "Sunday"
elif dow == 1: return "Monday"
elif dow == 2: return "Tuesday"
elif dow == 3: return "Wednesday"
elif dow == 4: return "Thursday"
elif dow == 5: return "Friday"
elif dow == 6: return "Saturday"
else: return "Error!"
try:
m = int(raw_input("What month were you born in (1-12)? "))
if not 1 <= m <= 12: raise Exception("There are no months with a number higher than 12!")
d = int(raw_input("On what date were you born on? "))
y = int(raw_input("What year were you born in? "))
print("\nYou were born on a %s!" % day_of_week(m,d,y))
except ValueError:
print("You need to enter a number!")