类型错误:不支持-:“int”和“list”的操作数类型

2024-05-23 15:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python创建一个程序,它将使用Zeller算法http://en.wikipedia.org/wiki/Zeller%27s_congruence告诉您一周中您出生的那一天,但是它给了我这个错误

TypeError: unsupported operand type(s) for -: 'int' and 'list'

为什么?

date = raw_input ("Introduce here the day, month and year you were born like this: DDMMYYYY")

if date.isdigit() and len(date) == 8:
    day = date[0:2]
    month = date[2:4]
    year = date[4:8]
    day = int(day)
    month = int(month)
    year = int(year)
    result = (day + (month + 1) * 2.6, + year % 100 + (year % 100) / 4 - 2 * [year / 100]) % 7

(这是我自己创建的第一个程序,请友好点;)


Tags: andorg程序算法httpdatewikiwikipedia
2条回答

我认为2 * [year / 100]应该是圆括号而不是方括号,否则它表示您要创建一个元素列表:

(year % 100) / 4 - 2 * (year / 100))
                       ^          ^ change [] to ()

在回答你的直接问题时发生的事情已经被@mellamokb和评论回答了。。。

不过,我要指出的是,Python已经有了这个内置功能,这将使它更容易:

from datetime import datetime
d = datetime.strptime('1312981', '%d%m%Y')
# datetime(1981, 12, 13, 0, 0)

然后,您可以更容易地对实际为datetime的对象执行计算,而不是对字符串进行计算。。。

相关问题 更多 >