# -*- 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,你應該怎么做?"""#測試題5dict1 = {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_passwordwhile True:name = input('請輸入用戶名:');if name in dict_user_password.keys():print('此用戶已經被占用,請重新輸入:')continueelse:break;password = input('請輸入密碼:')dict_user_password[name] = passwordprint('注冊成功')def login_user():global dict_user_passwordwhile True:name = input('請輸入用戶名:')if name in dict_user_password.keys():break;else:print('用戶名不存在,請重新輸入:')continuepassword = 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('指令輸入有誤!')