• 1.4


      1 # -*- coding: utf-8 -*-
      2 """
      3 扩展需求:
      4 1.用户下一次登录后 输入用户名密码 直接回到上次得状态 即上次消费得余额 物品 再次登录可继续购买
      5 2.允许查询之前得消费记录
      6 """
      7 import os
      8 
      9 goods = [
     10     {"name": "电脑", "price": 1999},
     11     {"name": "鼠标", "price": 10},
     12     {"name": "游艇", "price": 20},
     13     {"name": "美女", "price": 998}
     14 ]
     15 
     16 
     17 def show_goods():
     18     # 展示商品列表
     19     print('展示商品列表'.center(20,'-'))
     20     for index, i in enumerate(goods):
     21         print('%d. %s   %d' % (index, i['name'], i['price']))
     22 
     23 
     24 def judge_is_buy():
     25     # 判断用户曾经是否购买过商品
     26     if os.path.exists('last_goods_new.txt'):
     27         last_history = []
     28         f = open('last_goods_new.txt', 'r', encoding='utf-8')
     29         for line in f:
     30             last_history = eval(line)
     31         print('这是您第33[1;31m%d33[0m次登录,您上次消费余额33[1;31m%s33[0m' % (last_history[0]+1, last_history[1]))
     32         for index, i in enumerate(last_history[2], 1):
     33             print('%d. %s  %s' % (index, i['name'], i['price']))
     34         return last_history[0], last_history[1]
     35     else:
     36         return False
     37 
     38 
     39 def save_to_file(salary, buy_goods, buy_count):
     40     # 本次购买商品的记录存到文件中
     41     f = open('last_goods_new.txt', 'a', encoding='utf-8')
     42     f.write('%d,%s,%s
    ' % (buy_count, salary, buy_goods))
     43     f.close()
     44 
     45 
     46 def show_buy_goods(salary, buy_goods, buy_count):
     47     # 展示本次购买的商品
     48     print('本次:您够买了33[1;31m%s33[0m件商品,余额为33[1;31m%s33[0m' % (len(buy_goods),salary))
     49     for index, i in enumerate(buy_goods, 1):
     50         print('%d. %s  %d' % (index, i['name'], i['price']))
     51     save_to_file(salary, buy_goods, buy_count)
     52 
     53 
     54 def search_history(history_num):
     55     # 查询购买记录
     56     f = open('last_goods_new.txt', 'r', encoding='utf-8')
     57     for line in f:
     58         if history_num == eval(line)[0]:
     59             print(eval(line))
     60             print('您第%s次购买了:'%history_num)
     61             for line in eval(line)[2]:
     62                 print('%s'%line['name'])
     63             break
     64 
     65 
     66 def buy_goods_now(salary, buy_count):
     67     # 当前时刻 购买商品
     68     show_goods()
     69     buy_goods = []
     70     while True:
     71         choice = input('请选择购买商品的编号(输入q表示退出,输入x表示查询历史购买情况):').strip()
     72         if choice.isdigit():
     73             choice = int(choice)
     74             if choice < len(goods):
     75                 if salary >= goods[choice]['price']:
     76                     buy_goods.append(goods[choice])
     77                     salary -= goods[choice]['price']
     78                     print('商品33[1;35;46m%s33[0m已加入购物车' % goods[choice]['name'])
     79                     continue
     80                 else:
     81                     print('您的余额不足,请重新选择商品')
     82                     continue
     83             else:
     84                 print('编号不在商品范围内,请重新输入')
     85                 continue
     86         elif choice == 'q':
     87             show_buy_goods(salary, buy_goods, buy_count)
     88             exit()
     89         elif choice == 'x':
     90             if os.path.exists('last_goods_new.txt'):
     91                 history_num = input('请输入历史纪录编号:').strip()
     92                 if history_num.isdigit():
     93                     history_num = int(history_num)
     94                     search_history(history_num)
     95                 else:
     96                     print('请重新选择')
     97 
     98         else:
     99             print('请重新输入')
    100             continue
    101 
    102 
    103 def login(username,password):
    104     # 登录认证
    105     _username = "alice"
    106     _password = "123"
    107     if username == _username and password == _password:
    108         print('登录成功'.center(20, '-'))
    109         data = judge_is_buy()
    110         if data:
    111             buy_count = data[0]+1
    112             salary = data[1]
    113             buy_goods_now(salary, buy_count)
    114         else:  # 用户第一次登录
    115             while True:
    116                 salary = input('请输入工资:').strip()
    117                 if salary.isdigit():
    118                     salary = int(salary)
    119                 elif len(salary.split('.')) == 2 and salary.split('.')[0].isdigit() and salary.split('.')[1].isdigit():
    120                     salary = float(salary)
    121                 else:
    122                     print('请重新输入工资:')
    123                     continue
    124                 buy_goods_now(salary, 1)
    125     else:
    126         return False
    127 
    128 
    129 def main():
    130     # 主函数
    131     while True:
    132         username = input('请输入用户名(输入q表示退出):').strip()
    133         if username == 'q':
    134             break
    135         password = input('请输入密码(输入q表示退出):').strip()
    136         if password == 'q':
    137             break
    138         if not login(username, password): continue
    139 
    140 
    141 if __name__ == '__main__':
    142     main()
  • 相关阅读:
    JChartFree创建饼形图
    JFreeChart设置点的颜色
    JChartFree使用散点图
    JChartFree常用数据集
    博客园安家了
    在Android中什么是异步执行;
    XmlPullParserException
    构造器的执行顺序
    Sqlite之contentProvider
    使用java获取歌曲的属性
  • 原文地址:https://www.cnblogs.com/alice-bj/p/8454012.html
Copyright © 2020-2023  润新知