变量与基本数据类型

嵌套取值操作

1. 请取出第一个学生的第一个爱好

students_info = [['egon', 18, ['play', ]], ['alex', 18, ['play', 'sleep']]]
print(students_info[0][2][0])

2. 针对字典,请取出取公司名

info = {
    'name': 'egon',
    'hobbies': ['play', 'sleep'],
    'company_info': {
        'name': 'Old boy',
        'type': 'education',
        'emp_num': 40,
    }
}
#  请取出取公司名:
print(info['company_info']['name'])

3. 针对下述类型,取第二个学生的第二个爱好

students = [
    {'name': 'alex', 'age': 38, 'hobbies': ['play', 'sleep']},
    {'name': 'egon', 'age': 18, 'hobbies': ['read', 'sleep']},
    {'name': 'wupeiqi', 'age': 58, 'hobbies': ['music', 'read', 'sleep']},
]

print(students[1]['hobbies'][1])

附加题

1. 输入账号密码完成验证,验证通过后输出"登录成功"

while True:
    account = input('your account:')
    password = input('your password')
    if account == 'Avery':
        if password == '369':
            print('登陆成功')
            exit()
        else:
            print('密码输入错误,请重新输入')
            continue
    else:
        print('账号输入错误,请重新输入')

2. 可以登录不同的用户

account_dic = {
    'Avery': '369',
    'Kaia': '123'
}
while True:
    account = input('your account:')
    password = input('your password')
    if account in account_dic:
        if password == account_dic[account]:
            print('登陆成功')
            exit()
        else:
            print('密码输入错误,请重新输入')
            continue
    else:
        print('账号输入错误,请重新输入')
原文地址:https://www.cnblogs.com/avery-w/p/14192842.html