【发布时间】:2021-09-23 11:20:38
【问题描述】:
我是 python 新手,我想知道为什么我的程序不工作。第 23 行的错误是“IndexError: list index out of range” (这个节目是为明天我最好朋友的生日准备的)
import datetime
import tkinter
from tkinter import *
t = Tk()
t.resizable(0, 0)
t.title("Sana's birthday!!!")
t.geometry('200x200')
current_date = datetime.date.today().strftime('%Y-%m-%d')
current_date_lst = current_date.split('-')
l1 = Label(t, text='Sana enter your birthday in yyyy-mm-dd format:').grid(row=1, column=1)
l2 = Label(t, text='Name of your birthday legend?:').grid(row=2, column=1)
b_date = tkinter.Entry(t)
b_date.grid(row=1, column=2)
name = tkinter.Entry(t)
name.grid(row=1, column=2)
b_date = b_date.get().split( '-' )
if current_date_lst[1] == b_date[1] and current_date_lst[2] == b_date[2]:
age = int(current_date_lst[0]) - int(b_date[0])
ordinal_suffix = {1: 'st', 2:'nd', 3:'rd', 11:'th', 12:'th', 13:'th'}.get(age % 10 if not 10<age<=13 else age % 14, 'th')
print(f" It's {name}'s {age}{ordinal_suffix} Birthday!")
else:
print('Sorry, today is not your birthday:(')
mainloop()
错误:
if current_date_lst[1] == b_date[1] and current_date_lst[2] == b_date[2]:
IndexError: list index out of range
【问题讨论】:
标签: python list datetime tkinter indexing