[py]py输入判断-字典

来源:互联网 发布:网络潮语大全2016霸气 编辑:程序博客网 时间:2024/06/15 16:27
1.
dict = {'Name': 'Zara', 'Age': 27}#默认返回Noneprint  dict.get('Names')#可以自定义返回hiprint  dict.get('Names','hi')

2.打印菜单 输入选项判断选项是否合法

import sysdef menu():    print '''    1,增加    2,删除    3,修改    4,查询    5,显示所有    6,退出程序    '''    op=raw_input("请输入(1/2/3/4/5/6)> ")    return opdef txl_add():    print "增加"def txl_error():    print '当前的输入不在选项内,请重新输入'def txl_exit():    sys.exit(0)ops={    '1':txl_add,    # '2':txl_delete,    # '3':txl_modified,    '6':txl_exit,}def main():    while True:        op=menu()        ops.get(op,txl_error)()



3,连接数据库

dbconfig.py

imoprt MySQLdbdbconfig={    'host': '192.168.14.132',    'user': 'root',    'passwd': '123456',    'db': 'txl'}conn=MySQLdb.connet(**dbconfig)cursor=conn.cursor()


原创粉丝点击