购物车
#账户信息 egon|egon123 jason|jason123 tank|tank123 alex|alex123 eric|eric123 # 需求: # 用户名和密码存放于文件中,格式为:egon|egon123 # 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序 # 允许用户根据商品编号购买商品 # 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 # 可随时退出,退出时,打印已购买商品和余额 with open(r'G:\pythonlearning\account_info', mode='r', encoding='utf-8') as f: res = f.readlines() # 取出文件中的内容,并放入列表中 account_info = {} for i in res: a = i.split('|') # 分隔用户名和密码 account_info[a[0]] = a[1].strip() # 以用户名:密码的形式造一个字典 n = 0 while True: if n == 3: answer = input('是否继续Y/N') if answer.lower() == 'y': n = 0 else: break if n <= 3: name = input('请输入用户名:') if name in account_info: password = input('请输入密码:') if account_info[name] == password: print('登录成功') break else: n += 1 print('密码输入错误,请重新输入') else: print('账号不存在') salary = int(input('请输入你的工资:')) goods_list = {'apple': 10, 'tesla': 100000, 'mac': 3000, 'lenovo': 30000, 'chicken': 10, } # print(goods_list) print('序号\t商品名称') n = 1 d = {} for i in goods_list: print('%s\t\t%s' % (n, i)) d[n] = i n += 1 buy_already = [] print(d) mon_left = salary while True: shopping_number = int(input('请输入您想购买的商品的序号:')) if goods_list[d[shopping_number]] <= salary: mon_left = mon_left - goods_list[d[shopping_number]] # print(d[shopping_number]) buy_already .append(d[shopping_number]) # print(buy_already) else: print('余额不够') leave_or_not = input('请问你是否想退出?Y/N') if leave_or_not.lower() == 'y': print(buy_already, mon_left) break