映像和集合类型

时间:2022-02-01 20:47:25

 创建字典
dict1 = {"a":1,"b":2}
dict2 = dict((['x',1],['y',2]))
dict3 = {}.fromkeys(('x','y'),-1)

通过key访问字典
for key in dict1.keys():
    print "key:%s value:%s" % (key,dict1[key])

删除字典
del dict1["a"]
dict1.clear()
dict1.pop('a')

实现用户注册登录的功能
#-*-coding:UTF-8-*-
'''
Created on 2010-4-15

@author: Administrator
'''
db = {}
def newUser():
    print "用户注册"
    print "="*50
    while True:
        name = raw_input("请注册用户名:")
        if db.has_key(name):
            print "该用户已被注册!"
            continue
        else:
            break
    db[name] = raw_input("请输入密码:")
    print "恭喜你注册成功!"

def oldUser():
    print "用户登录"
    print "="*50
    name = raw_input("请输入用户名:")
    pwd = raw_input("请输入密码:")
    if not db.has_key(name):
        print "该用户不存在!"
    else:
        if pwd != db[name]:
            print "密码错误!"
        else:
            print "恭喜你登录成功!"

def showMenu():
    print "(N)用户注册/n(E)用户 登录/n(Q)退出系统"
    while True:
        choice = raw_input(">>");
        if choice == "N":
            newUser()
        elif choice == 'E':
            oldUser()
        elif choice == 'Q':
            break
        else:
            continue

if __name__ == '__main__':
    showMenu()
   
   
集合类型
创建集合类型set(),frozenset()