ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-03-09 21:01:32  阅读:311  来源: 互联网

标签:题及 dict1 Python cmd dict user 课后 input password


# -*- 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('指令输入有误!')

  

标签:题及,dict1,Python,cmd,dict,user,课后,input,password
来源: https://www.cnblogs.com/alimy/p/10502968.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有