函数append并没有添加到我的列表中,而是改变了原来的

问题描述 投票:0回答:1

导入日期时间 从日期时间导入日期时间 A = 真 而真实的: 而真实的: 尝试:

        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

在你的沐浴日和你的名字之间(dd-mm-yyyy,名字):11-11-2001,a

a你22岁

{'姓名':['a'],'日期':['11-11-2001']}

将你的沐浴日和你的名字添加到列表 dd-mm-yyyy,名字)

如果要删除列表,请输入(清除):11-11-2001,AA

AA你22岁

{'姓名':['AA'],'日期':['11-11-2001']}

将你的沐浴日和你的名字添加到列表 dd-mm-yyyy,名字)

如果你想删除列表输入(清除):

python append
1个回答
0
投票

列表应该在 while 循环之外初始化。所以你应该在 while 循环之前移动以下内容:

    people = {
        'the name':[],
        'the date':[]
    }

否则,在编写代码时,您每次都在重新创建新列表。

© www.soinside.com 2019 - 2024. All rights reserved.