• Python_常见练习


     1 #!/usr/bin/env python3
     2 # _*_ coding:utf-8 _*_
     3 
     4 # 登录接口, 三次登录机会
     5 
     6 flag = 0
     7 
     8 user_data = {'username': '13564957378',
     9              'password': '123456'}
    10 
    11 while True:
    12 
    13     username = str(input('请输入账号:')).strip()
    14     if username != str(user_data['username']):
    15         print('输入的账号不存在')
    16         continue
    17 
    18     # 读取lock.txt文件数据
    19     lock_data = []
    20     try:
    21         with open('lock.txt', 'r') as f:
    22             for line in f.readlines():
    23                 lock_data.append(str(line).strip('
    '))
    24     except Exception:
    25         print('文件读取失败')
    26         raise
    27 
    28     while flag < 3:
    29         # 判断输入账号是否被锁住
    30         if username in lock_data:
    31             print('您的账号已被锁住,请联系管理员')
    32             break
    33         else:
    34             password = str(input('请输入密码')).strip('
    ')
    35             if username == str(user_data['username']) and password == str(user_data['password']):
    36                 print('{0} 登录成功!欢迎进入qc系统'.format(username))
    37                 import sys
    38                 sys.exit()
    39             else:
    40                 print('您还有{0}机会'.format(str(2-flag)))
    41         flag += 1
    42     else:
    43         # 写入三次登录失败的账号进入lock文件
    44         with open('lock.txt', 'w') as f:
    45             f.writelines('{0}'.format(username)+'
    ')
    46         break
    登录>三次机会
    1 #!/usr/bin/env python3
    2 # _*_ coding:utf-8 _*_
    3 
    4 # 九九乘法表
    5 
    6 for x in range(1, 10):
    7     for y in range(1, x+1):
    8         print('{0}*{1}={2}'.format(x, y, x*y), end=' ')
    9     print()
    99乘法表
     1 #!/usr/bin/env python3
     2 # _*_ coding:utf-8 _*_
     3 from __future__ import print_function
     4 
     5 
     6 """
     7 使用#号输出一个长方形,用户可以指定宽和高
     8 """
     9 
    10 width = int(str(input('请输入宽度')).strip())
    11 height = int(str(input('请输入长度')).strip())
    12 
    13 if isinstance(width, int) and isinstance(height, int):
    14     if width > 0 and height > 0:
    15         for x in range(1, width+1):
    16             for y in range(1, height+1):
    17                 print('#', end='')
    18             print()
    使用#号输出一个长方形
     1 #!/usr/bin/env python3
     2 # _*_ coding:utf-8 _*_
     3 
     4 # 直角三角形
     5 
     6 flag = 5
     7 
     8 for a in range(1, flag+1):
     9     for b in range(1, a+1):
    10         print('*', end='')
    11     print()
    直角三角形
     1 #!/usr/bin/env python3
     2 # _*_ coding:utf-8 _*_
     3 
     4 # 倒直角三角
     5 
     6 flag = 5
     7 
     8 while flag > 0:
     9     
    10     temp = flag
    11     while temp > 0:
    12         print('*', end='')
    13         temp -= 1
    14     print()
    15     
    16     flag -= 1
    倒三角
     1 #!/usr/bin/env python3
     2 # _*_ coding:utf-8 _*_
     3 import sys
     4 
     5 # 购物车,LIST TYPE
     6 trolley = []
     7 
     8 # 工资默认5000,INT TYPE
     9 salary = 5000
    10 
    11 FLAG = True
    12 
    13 STATUS = ['SUCCESS', 'FAIL', 'ERROR', 'WARN']
    14 
    15 goods = {
    16     '核心编程2': 60,
    17     '核心编程3': 65,
    18     '流畅的Python': 90,
    19     'Python学习手册': 100,
    20     'mac book': 9000
    21 }
    22 
    23 
    24 def log_info(msg):
    25     return print(msg)
    26 
    27 
    28 def cutting_line():
    29     return log_info('*******************************************************')
    30 
    31 while FLAG:
    32 
    33     # 输入Y则进行购物操作,否则退出程序
    34     order_input = str(input('是否进行购物[Y/N]!')).strip()
    35 
    36     if order_input not in ['Y', 'N']:
    37         log_info('{0} 指令输入有误,请重新输入!'.format(STATUS[2]))
    38     elif order_input == 'Y':
    39 
    40         cutting_line()
    41         log_info('编号:%-5s 商品名称:%-12s  商品价格:(元)' % ('	', '	'))
    42 
    43         good_items = 1
    44         shopping_goods = []
    45 
    46         # 商品列表清单
    47         for good, price in goods.items():
    48 
    49             log_info('%-10s	 %-18s 	 %s33[0m' % (good_items, good, price))
    50             shopping_goods.append(good)
    51             good_items += 1
    52 
    53         # 购买逻辑
    54         while FLAG:
    55 
    56             shopping_order = str(input('请选择需要购买的货物!')).strip()
    57             if shopping_order not in shopping_goods:
    58                 log_info('{0} 指令输入有误,请重新输入!'.format(STATUS[2]))
    59             else:
    60                 if salary < int(goods[shopping_order]):
    61                     log_info('{0} 金额不足,请重新选择!'.format(STATUS[1]))
    62                     break
    63                 elif salary >= int(goods[shopping_order]):
    64                     trolley.append(shopping_order)
    65                     salary -= int(goods[shopping_order])
    66                     log_info('已加入{0}到你的购物车, 当前余额:{1}'.format(shopping_order, salary))
    67                     going_oder = str(input('是否继续购买?')).strip()
    68                     if going_oder != 'Y':
    69                         FLAG = False
    70         else:
    71             cutting_line()
    72             log_info('购买的商品为:')
    73             for item, good in enumerate(trolley):
    74                 log_info('{0}       {1}'.format(item+1, good))
    75             else:
    76                 log_info('您的余额还剩:{0}'.format(str(salary)))
    77 
    78     else:
    79         cutting_line()
    80         log_info('{0} 欢迎下次光临!'.format(STATUS[0]))
    81         sys.exit()
    购物车
     1 #!/usr/bin/env python3
     2 # _*_ coding:utf-8 _*_
     3 # 三级菜单
     4 #   打印省、市、县三级菜单
     5 #   可返回上一级
     6 #   可随时退出程序
     7 
     8 menu = {
     9     '山东': {
    10         '青岛': ['四方', '黄岛', '崂山', '李沧', '城阳'],
    11         '济南': ['历城', '槐荫', '高新', '长青', '章丘'],
    12         '烟台': ['龙口', '莱山', '牟平', '蓬莱', '招远']
    13     },
    14     '江苏': {
    15         '苏州': ['沧浪', '相城', '平江', '吴中', '昆山'],
    16         '南京': ['白下', '秦淮', '浦口', '栖霞', '江宁'],
    17         '无锡': ['崇安', '南长', '北塘', '锡山', '江阴']
    18     },
    19     '浙江': {
    20         '杭州': ['西湖', '江干', '下城', '上城', '滨江'],
    21         '宁波': ['海曙', '江东', '江北', '镇海', '余姚'],
    22         '温州': ['鹿城', '龙湾', '乐清', '瑞安', '永嘉']
    23     },
    24     '安徽': {
    25         '合肥': ['蜀山', '庐阳', '包河', '经开', '新站'],
    26         '芜湖': ['镜湖', '鸠江', '无为', '三山', '南陵'],
    27         '蚌埠': ['蚌山', '龙子湖', '淮上', '怀远', '固镇']
    28     },
    29     '广东': {
    30         '深圳': ['罗湖', '福田', '南山', '宝安', '布吉'],
    31         '广州': ['天河', '珠海', '越秀', '白云', '黄埔'],
    32         '东莞': ['莞城', '长安', '虎门', '万江', '大朗']
    33     }
    34 }
    35 
    36 FLAG = True
    37 
    38 
    39 def log_info(msg):
    40     print(msg)
    41 
    42 while FLAG:
    43 
    44     # 省一级菜单显示
    45     province = list(menu.keys())
    46 
    47     log_info('#####省一级菜单#####')
    48     for index, state in enumerate(province):
    49         print('{0}      {1}'.format(index+1, state))
    50     else:
    51         going_order = str(input('first 是否继续?Y/N')).strip()
    52         if going_order != 'Y':
    53             FLAG = False
    54 
    55     # 市二级菜单
    56     while FLAG:
    57 
    58         county_order = str(input('选择菜单?')).strip()
    59 
    60         log_info('#####市二级菜单#####')
    61         county = []
    62         for value in menu[county_order]:
    63             county.append(value)
    64         for index, prefecture in enumerate(county):
    65             print('{0}      {1}'.format(index + 1, prefecture))
    66         else:
    67             going_order = str(input('second 是否继续?Y:继续,N:返回上一级,Q:退出')).strip()
    68             if going_order == 'Y':
    69 
    70                 while FLAG:
    71 
    72                     # 县三级菜单
    73                     clark_order = str(input('选择菜单?')).strip()
    74                     log_info('#####县三级菜单#####')
    75                     clark = []
    76                     for value in menu[county_order][clark_order]:
    77                         clark.append(value)
    78                     for index, value in enumerate(clark):
    79                         print('{0}      {1}'.format(index + 1, value))
    80                     else:
    81                         going_order = str(input('third 是否继续?Y:继续,N:返回上一级,Q:退出')).strip()
    82                         if going_order == 'Y':
    83                             continue
    84                         elif going_order == 'N':
    85                             break
    86                         else:
    87                             FLAG = False
    88 
    89             elif going_order == 'N':
    90                 break
    91             else:
    92                 FLAG = False
    三级菜单
     1 #!/usr/bin/env python3
     2 # _*_ coding:utf-8 _*_
     3 # ========================================================
     4 # Module         :  list_set
     5 # Author         :  luting
     6 # Create Date    :  2018/3/13
     7 # Amend History  :  2018/3/13
     8 # Amended by     :  luting
     9 # ========================================================
    10 from __future__ import print_function
    11 import functools
    12 '''列表联动'''
    13 
    14 
    15 def list_sorted(func):
    16 
    17     @functools.wraps(func)
    18     def wrapper(*args):
    19         if isinstance(args[0], list) and isinstance(args[1], list):
    20             return sorted(args[1], key=lambda x: args[0].index(x))
    21 
    22     return wrapper
    23 
    24 
    25 @list_sorted
    26 def test(a, b):
    27     pass
    28 print(test([3, 5, 6, 1, 7, 4], [1, 6, 3]))
    列表联动
  • 相关阅读:
    使用XE7并行库中的TTask(转)
    Delphi xe7并行编程快速入门(转)
    Pre-compile (pre-JIT) your assembly on the fly, or trigger JIT compilation ahead-of-time (转)
    使用RemObjects Pascal Script (转)
    Remobjects SDK 服务器搭建
    toString()方法
    环境变量
    jstl标签学习
    SQL的各种join
    Mybatis的一些配置
  • 原文地址:https://www.cnblogs.com/xiaoxiaolulu/p/8528222.html
Copyright © 2020-2023  润新知