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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14042291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:14:46  来源:igfitidea点击:

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

pythonlistinttypeerror

提问by Esther28

I'm trying to create a program in python that will tell you the day of the week you were born using the Zeller algorithm http://en.wikipedia.org/wiki/Zeller%27s_congruencebut it's giving me this error

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

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

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

Why is that?

这是为什么?

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

(It's the first program I create by myself, so be nice please ;) )

(这是我自己创建的第一个程序,所以请保持礼貌;))

采纳答案by Jon Clements

What's happening in answer to your direct question has been answered by @mellamokb and comments...

@mellamokb 和评论已经回答了您直接问题的答案...

However, I would point out that Python already this builtin and would make it easier:

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

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

Then you can perform calculations more easily on an object that is actually a datetimerather than co-erced strings...

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

回答by mellamokb

I think 2 * [year / 100]should be parentheses instead of brackets, otherwise it indicates you want to make a single-element list:

我认为2 * [year / 100]应该是圆括号而不是方括号,否则表示您要制作单元素列表:

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