sky = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸']earth = ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥']
while True:try:skynum = 2earthnum = 6year = int(input())gap = year - 1906
earthnum = earthnum + gapif (earthnum >= 0):loop = gapearthnum %= 12while (loop > 10):loop -= 10skynum += loop
elif (earthnum < 0):loop = -gapearthnum %= 12while (loop > 10):loop -= 10skynum -= loopprint(sky[skynum]+earth[earthnum])
except: break在IDE裡執行都沒問題求解
你的skynum可能會超過10,例如輸入是1914 1915 1916的時候