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_congruence but it's giving me this error
我正在尝试用python创建一个程序,它会告诉你你出生的那一天是哪天,使用Zeller算法http://en.wikipedia.org/wiki/zeller%27s_但是它给了我这个错误
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 ;) )
(这是我自己设计的第一个程序,所以请好好表现;)
2 个解决方案
#1
4
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 datetime
rather than co-erced strings...
然后,您就可以更容易地对一个实际上是datetime(日期时间)而不是共写字符串的对象执行计算……
#2
3
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 ()
#1
4
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 datetime
rather than co-erced strings...
然后,您就可以更容易地对一个实际上是datetime(日期时间)而不是共写字符串的对象执行计算……
#2
3
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 ()