标签:false als ret inpu 过程 append eth blog passwd
一,作者介绍
吴海东:My Blog:http://www.cnblogs.com/whd-672701
二,程序说明
购物车程序:
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录
三,程序前准备
创建一个购物物品文件“shopping”,物品可自行定义。
创建一个存放已购买物品的文件“lock_file”,开始默认为空。
四,程序脚本
shangpin = open("shopping",‘r‘,encoding="utf-8") shangpin = list(shangpin) shopping_list = [] def test(): for index, i in enumerate(shangpin): print(index, i) mai = input("Want to buy what?>>>:") return mai def test_1(): jin = input("Please enter the amount:") if jin.isdigit(): jin = int(jin) loginSucces = False while True: mai = test() if mai.isdigit(): mai = int(mai) if mai < len(shangpin) and mai >= 0: kemai = shangpin[mai] kemai = kemai.strip().split(‘,‘) kemaii = int(kemai[1]) if kemaii <= jin: shopping_list.append(kemai) jin -= kemaii print("\033[32;1mBuy the goods:%s,The balance remaining:%s\033[0m" % (kemai, jin)) loginSucces = False while True: jixu = input("继续购买按y,或是退出按q:") if jixu.isalpha(): jixu = str(jixu) if jixu == ‘y‘: break if loginSucces == True:continue elif jixu == ‘q‘: print("\033[31;1m-----You bought the following goods altogether----") for p in shopping_list: print(p) f = open("lock_file", ‘a+‘, encoding="utf-8") f.write("{\"%s\":{\"name\":\"%s\",\"shopping\":%s,\"remaining\":%s}}\n" % (str(username), str(username), str(shopping_list), str(jin))) f.close() if loginSucces == True: break print("The balance remaining:%s\033[0m" % jin) exit() if loginSucces == True: break continue else: print("Insufficient balance to buy:%s" % jin) loginSucces = False while True: jixu = input("继续购买按y,或是退出按qq:") if jixu.isalpha(): jixu = str(jixu) if jixu == ‘y‘: break elif jixu == ‘q‘: print("\033[31;1m-----You bought the following goods altogether----") for p in shopping_list: print(p) f = open("lock_file", ‘a+‘, encoding="utf-8") f.write("{\"%s\":{\"name\":\"%s\",\"shopping\":%s,\"remaining\":%s}}\n" % ( str(username), str(username), str(shopping_list), str(jin))) f.close() print("The balance remaining:%s\033[0m" % jin) exit() if loginSucces == True:break def test_2(): for date in dataa: if username in date: print(dataa[username]) else: continue jin = dataa[username]["remaining"] jine = jin jin = str(jin) if jin.isdigit(): jin = int(jin) loginSucces = False while True: mai = test() if mai.isdigit(): mai = int(mai) if mai < len(shangpin) and mai >= 0: kemai = shangpin[mai] kemai = kemai.strip().split(‘,‘) kemaii = int(kemai[1]) if kemaii <= jin: aa = dataa[username]["shangpin"] aaa = aa aaa = str(aaa) shopping_list = dataa[username]["shangpin"] shopping_list.append(kemai) print(shopping_list) shopping_li = shopping_list shopping_li = str(shopping_li) jin -= kemaii jin = str(jin) jine = str(jine) print("\033[32;1mBuy the goods:%s,The balance remaining:%s\033[0m" % (kemai, jin)) loginSucces = False while True: jixu = input("继续购买按y,或是退出按q:") if jixu.isalpha(): jixu = str(jixu) if jixu == ‘y‘: break if loginSucces == True: continue elif jixu == ‘q‘: print("\033[31;1m-----You bought the following goods altogether----") for p in shopping_list: print(p) with open("lock_file","r",encoding="utf-8") as f: lines = f.readlines() with open("lock_file","w",encoding="utf-8") as f_w: for line in lines: if username in line: line = line.replace(aaa,shopping_li) f_w.write(line) f_w.close() with open("lock_file", "r", encoding="utf-8") as f: lines = f.readlines() with open("lock_file", "w", encoding="utf-8") as f_q: for line in lines: if username in line: line = line.replace(jine,jin) f_q.write(line) f_q.close() print("The balance remaining:%s\033[0m" % jin) exit() if loginSucces == True: break username = input(‘xinuser:‘).strip() with open("lock_file",‘r‘,encoding="utf-8") as f: for data in f: if len(data) != 0: dataa = eval(data) for date in dataa: daa = date if username in daa: break elif username not in daa: continue test_2() else: password = input(‘passwd:‘).strip() test_1()
标签:false als ret inpu 过程 append eth blog passwd
原文地址:http://www.cnblogs.com/whd-672701/p/7152623.html