0

我不知道出了什么问题。我告诉我在哪里有一个字符串格式错误,a = y % 19然后它还告诉我在调用main(). 不知道如何解决这个问题,因为我似乎main()正确地调用了该函数并且string看起来很好。

def main():

    y = input("Enter year: ")
    print ("y = ", y)

    a = y % 19
    print ("a = ", a)

    b = y / 100
    print ("b = ", b)

    c = y % 100
    print ("c = ", c)

    d = b / 4
    print ("d = ", d)

    e = b % 4
    print ("e = ", e)

    g = (8 * b + 13) / 25
    print ("g = ", g)

    h = (19 * a + b - d - g + 15) / 30
    print ("h = ", h)

    j = c / 4
    print ("j = ", j)

    k = c % 4
    print ("k = ", k)

    m = (a + 11 * h) / 319
    print ("m = ", m)

    r = (2 * e + 2 * j - k - h + m + 32)  % 7
    print ("r = ", r)

    n = (h - m + r + 90) / 25
    print ("n = ", n)

    p = (h - m + r + n + 19) % 32
    print ("p = ", p)

    print ("In ", y, "Easter Sunday is on", p,)
    if (n == 3):
        print ("March")
    if (n == 4):
        print ("April")

main()
4

3 回答 3

4

看起来您使用的是 Python 3。如果是这样,第一个问题是y字符串(如"2013"),而不是数字(如2013)。该%操作对字符串的意义与对数字的意义不同。您必须y通过说转换为整数y = int(input('....'))

不确定模块的问题是什么;您可以发布确切的错误消息以及您运行代码的方式吗?(是在 IDLE 中,通过调用python myscript.py,还是什么?)

于 2013-06-30T17:56:56.407 回答
1

input()返回一个字符串。您可能希望将其转换为intasx = int(input(...))

raw_input()如果您正在使用,您可能还想使用Python v2.x,它也返回string但不评估任何表达式。

在这里查看更多详情。

于 2013-06-30T18:01:31.323 回答
0
~$ python3 Test.py
Enter year: 2013
y =  2013
Traceback (most recent call last):
  File "Test.py", line 51, in <module>
    main()
  File "Test.py", line 6, in main
    a = y % 19
TypeError: not all arguments converted during string formatting

~$ python2.7 Test.py
Enter year: 2013
('y = ', 2013)
('a = ', 18)
('b = ', 20)
('c = ', 13)
('d = ', 5)
('e = ', 0)
('g = ', 6)
('h = ', 12)
('j = ', 3)
('k = ', 1)
('m = ', 0)
('r = ', 4)
('n = ', 4)
('p = ', 7)
('In ', 2013, 'Easter Sunday is on', 7)
April
于 2013-06-30T18:03:19.323 回答