【问题标题】:TypeError: unsupported operand type(s) for -: 'int' and 'list'TypeError: 不支持的操作数类型 -: 'int' 和 'list'
【发布时间】:2012-12-12 02:41:32
【问题描述】:

我正在尝试在 python 中创建一个程序,它会使用 Zeller 算法 http://en.wikipedia.org/wiki/Zeller%27s_congruence 告诉你你出生的星期几,但它给了我这个错误

TypeError: 不支持的操作数类型 -: 'int' 和 '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

(这是我自己创建的第一个程序,请多多关照;))

【问题讨论】:

  • [year / 100] 将其更改为:(year / 100),也在(day + (month + 1) * 2.6 中右括号在哪里? (删除,

标签: python list int typeerror


【解决方案1】:

@mellamokb 和 cmets 已回答您的直接问题时发生了什么...

但是,我要指出 Python 已经内置了这个并且会更容易:

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

然后,您可以更轻松地对实际上是 datetime 的对象执行计算,而不是强制字符串...

【讨论】:

    【解决方案2】:

    我觉得2 * [year / 100]应该是圆括号而不是方括号,否则说明你要制作单元素列表:

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

    【讨论】:

    • 2.6 后面加上, 也是一个错误。
    • 谢谢大家的回答!我做了这个和逗号的事情,它正在工作!
    猜你喜欢
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 2014-12-28
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多