import datetime
from datetime import datetime
A = True
while True:
try:
if A == True:
input_user_bd = input('inter your barth day and your name(dd-mm-yyyy,the name):')
else:
input_user_bd = input('add your barth day and your name to the list '
'dd-mm-yyyy,the name)\nif you want to delete the list enter(clear):')
date,name = input_user_bd.split(',')
bd = datetime.strptime(date, '%d-%m-%Y')
break
except ValueError or AttributeError:
print('error')
def calculate_age(bd):
today = datetime.now()
return today.year - bd
calculate_age(bd.year)
def print_age(th_name,age):
print(th_name,'you are',age,"years old")
print_age(name,calculate_age(bd.year))
people = {
'the name':[],
'the date':[]
}
people['the name'].append(name)
people['the date'].append(date)
print(people)
A = False
if input_user_bd == 'clear':
break
以下是命令行输入和输出:
interyour barth day and your name(dd-mm-yyyy,the name):11-11-2001,a
a你22岁
“名称”:['a'],'日期':[11 -11-2001']}
把你的生日和你的名字添加到列表中dd-mm-yyyy,名字)
如果要删除列表,请输入(清除):11-11-2001,AA
你22岁了
“名称”:['AA'],'日期':[11 -11-2001']}
把你的生日和你的名字添加到列表中dd-mm-yyyy,名字)
如果要删除列表,请输入(清除):
1条答案
按热度按时间62lalag41#
列表应该在while循环之外初始化。因此,您应该在while循环之前移动以下内容:
否则,在编写代码时,每次都在重新创建新列表。