# Lists.
months = [6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
# Functions for algorithm.
def yearcode(y):
"""generate year-code using algorithm"""
y = y % 100
y = y + (y / 4) % 7
return round(y)
def monthcode(m):
"""get month number from month-list"""
return months[monthin - 1]
def daycode(d):
"""simplify day number for efficiency"""
return d % 7
# Inputs.
dayayin = int(input("What Day in the Month?"))
monthin = int(input("What Month? E.g.- January is 1"))
yearin = int(input("What Year?"))
# Define variables for functions.
yearout = yearcode(yearin)
monthout = monthcode(monthin)
dayout = daycode(dayin)
# Final Add-Up and Output.
result = (dayout + monthout + yearout) % 7
print(weekdays[result])
错误是:“ParseError: bad input on line 17” 这个程序的目的是给出任何日期的星期几。正如你所看到的,它对我如何为我的利益给出函数的目的并不满意。我真的觉得我在这里错过了一些东西。
这是改进和工作版本(感谢您的帮助!)
# Lists.
months = [6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
# Fruitful Functions for Algorithm.
def yearcode(y):
"""Year Code Generator Algorithm"""
y = y % 100
y = y + (y / 4) % 7
return int(round(y))
def monthcode(m):
"""Retrieve Month Number from Month List"""
return months[m - 1]
def daycode(d):
"""Simplify Day Input for Efficiency"""
return d % 7
# Inputs.
dayin = int(input("What Day in the Month?"))
monthin = int(input("What Month? E.g.- January is 1"))
yearin = int(input("What Year?"))
# Define Variables for Functions.
yearout = yearcode(yearin)
monthout = monthcode(monthin)
dayout = daycode(dayin)
# Final Add-Up and Output.
result = int((dayout + monthout + yearout) % 7)
print(weekdays[result])