python 小甲鱼 好不好_[Python]小甲鱼Python视频第026课(字典:当索引不好用时2)课后题及参考解答...

# -*- coding: utf-8 -*-

"""

Created on Fri Mar 8 10:32:20 2019

@author: Administrator

"""

"""

测试题:

0. Python的字典是否支持一键(Key)多值(Value)?

不支持

1. 在字典中,如果试图为一个不存在的键(Key)赋值会怎样?

字典对象中会出现一个新的键值对

2. 成员资格操作符(in和not in)可以检查一个元素是否存在序列中,当然也可以用来检查一个键(Key)是否存在字典中,那么请问哪种的检查效率更高些?为什么?

检查一个键(Key)是否存在字典中的效率更高,通过查找hash值一步到位,不需要迭代或遍历

3. Python对键(Key)和值(Value)有没有类型限制?

对Value并没有啥限制

Key必须是能hash的对象(序列类型就不行)

4. 请目测下边代码执行后,字典dict1的内容是什么?

>>> dict1.fromkeys((1, 2, 3), ('one', 'two', 'three'))

>>> dict1.fromkeys((1, 3), '数字')

{

1:'数字',

3:'数字'

}

5. 如果你需要将字典dict1 = {1: 'one', 2: 'two', 3: 'three'}拷贝到dict2,你应该怎么做?

"""

#测试题5

dict1 = {1: 'one', 2: 'two', 3: 'three'};

dict2 = dict1;

dict3 = dict1.copy();

#动动手0,程序有点问题,没有检查input的输入能否为空

dict_user_password = dict({'0':'0'});

string1 = """|--- 新建用户:N/n ---|

|--- 登录账号:E/e ---|

|--- 退出程序:Q/q ---|

|--- 请输入指令代码:

""";

def ShowAndGetCmd():

global string1;

print(string1);

return input();

def add_user():

global dict_user_password

while True:

name = input('请输入用户名:');

if name in dict_user_password.keys():

print('此用户已经被占用,请重新输入:')

continue

else:

break;

password = input('请输入密码:')

dict_user_password[name] = password

print('注册成功')

def login_user():

global dict_user_password

while True:

name = input('请输入用户名:')

if name in dict_user_password.keys():

break;

else:

print('用户名不存在,请重新输入:')

continue

password = input('请输入密码');

if password == dict_user_password.get(name):

print('密码正确');

else:

print('密码错误');

while True:

input_cmd = ShowAndGetCmd()

if input_cmd == 'N' or input_cmd == 'n':

add_user();

elif input_cmd == 'E' or input_cmd == 'e':

login_user();

elif input_cmd == 'Q' or input_cmd == 'q':

break ;

else:

print('指令输入有误!')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值