#31453: 請問為甚麼這樣不對(py)


andy.lin61013@gmail.com (我是你爸)


sky = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸']
earth = ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥']

while True:
    try:
        skynum = 2
        earthnum = 6
        year = int(input())
        gap = year - 1906

        earthnum = earthnum + gap
        if (earthnum >= 0):
            loop = gap
            earthnum %= 12
            while (loop > 10):
                loop -= 10
            skynum += loop

        elif (earthnum < 0):
            loop = -gap
            earthnum %= 12
            while (loop > 10):
                loop -= 10
            skynum -= loop
        print(sky[skynum]+earth[earthnum])

    except: break
 
 
在IDE裡執行都沒問題
求解
#31470: Re: 請問為甚麼這樣不對(py)


cges30901 (cges30901)


sky = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸']
earth = ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥']

while True:
    try:
        skynum = 2
        earthnum = 6
        year = int(input())
        gap = year - 1906

        earthnum = earthnum + gap
        if (earthnum >= 0):
            loop = gap
            earthnum %= 12
            while (loop > 10):
                loop -= 10
            skynum += loop

        elif (earthnum < 0):
            loop = -gap
            earthnum %= 12
            while (loop > 10):
                loop -= 10
            skynum -= loop
        print(sky[skynum]+earth[earthnum])

    except: break
 
 
在IDE裡執行都沒問題
求解


你的skynum可能會超過10,例如輸入是1914 1915 1916的時候