# Author:Army lu import os def admin(): product_list = [] with open(‘products.txt‘,‘r‘,encoding=‘utf-8‘) as f: for line in f: product_list.append(line.strip(‘\n‘).split(‘,‘)) for i in range(len(product_list)): print(‘%s,%s,%s‘%(i,product_list[i][0],product_list[i][1])) admin_act = input(‘\n\n"A" for add\n"E" for edit\n"D" for delete\n"Q" for quite\nwhat do you want to do: ‘) if admin_act ==‘A‘: product_name = input(‘please input the product name:‘) product_price = input(‘please input the product price:‘) while not product_price.isdigit(): product_price = input(‘please input the product price:‘) product_new =[product_name,int(product_price)] product_list.append(product_new) elif admin_act ==‘E‘: edit_num = input(‘please input the number which you want to edit:‘) while not edit_num.isdigit(): edit_num = input(‘please input the number which you want to edit:‘) if int(edit_num) <= len(product_list)-1: edit_price = input(‘please input the price:‘) while not edit_price.isdigit(): edit_price = input(‘please input the price:‘) product_list[int(edit_num)][1] = int(edit_price) elif admin_act ==‘D‘: del_num = input(‘please input the number which you want to delete:‘) while not del_num.isdigit(): del_num = input(‘please input the number which you want to delete:‘) if int(del_num) <= len(product_list)-1: del product_list[int(del_num)] elif admin_act == ‘Q‘: exit() else: pass for i in range(len(product_list)): print(‘%s,%s,%s‘%(i,product_list[i][0],product_list[i][1])) user_comfirm = input(‘Please confirm the change (Y/N):‘) if user_comfirm == ‘Y‘: with open(‘products.txt‘, ‘w‘, encoding=‘utf-8‘) as f: for i in range(len(product_list)): f.write(‘%s,%s\n‘ % (product_list[i][0], product_list[i][1])) user_continue = input(‘Contine? (Y/N):‘) if user_continue == ‘Y‘: admin() def user(): #用户登录 user_list = [] user_name = input(‘Please input your name:‘) user_key = ‘‘ user_account = 0 user_exist = False user_info = [] with open(‘users.txt‘,‘r‘,encoding=‘utf-8‘) as f: for line in f: user_info = line.split(‘,‘) user_info[2] = int(user_info[2]) if user_name == user_info[0]: user_exist = True user_key = user_info[1] user_account = user_info[2] if input(‘Please input the key:‘) != user_key: print(‘Please check your key.‘) exit() print(‘========Welcome :%s======\nyour account: %d‘%(user_name,user_account)) account_add = input(‘Do you want to add your account? If Yes,please input the num:‘) if account_add.isdigit(): user_account += int(account_add) print(‘====== your account:%s‘%user_account) user_list.append(line.strip(‘\n‘).split(‘,‘)) if user_exist == False: user_key = input(‘Welcome new user,please input your key:‘) while len(user_key) == 0: user_key = input(‘Welcome new user,please input your key:‘) user_account = input(‘Please input your salary:‘) if user_account.isdigit(): user_account = int(user_account) else: print(‘Please input the right number.See you again!‘) exit() #生成产品清单 product_list = [] min_price = 1000000 with open(‘products.txt‘,‘r‘,encoding=‘utf-8‘) as f: for line in f: product_list.append(line.strip(‘\n‘).split(‘,‘)) for i in range(len(product_list)): product_list[i][1] = int(product_list[i][1] ) if min_price > product_list[i][1]:min_price = product_list[i][1] #循环购物 shopping_list=[] while user_account> min_price: for index, item in enumerate(product_list): print(index, item) print(‘your account:\033[31;1m%s\033[0m‘%user_account) choose_num = input(‘Please input the num of the productions or "q" for quit:‘) if choose_num.isdigit(): choose_num = int(choose_num) if choose_num <= len(product_list) and product_list[choose_num][1] <= user_account: shopping_list.append(choose_num) user_account -=product_list[choose_num][1] elif choose_num == ‘q‘: break else: print(‘Please input the right num!‘) # 写入日志 user_record =[user_name,user_key,user_account] if user_exist == True: for i in range(len(user_list)): if user_list[i][0] == user_name: user_list[i][2] = user_account break else: user_list.append(user_record) with open(‘users.txt‘, ‘w‘, encoding=‘utf-8‘) as f: for i in range(len(user_list)): f.write(‘%s,%s,%s\n‘%(user_list[i][0],user_list[i][1],user_list[i][2])) #打印清单 print(‘========Thanks for shopping======‘) print(‘your account:%s‘%user_account) print(‘your shoppinglist:‘) if len(shopping_list) == 0: print(‘you buy nothing!‘) else: for i in range(len(shopping_list)): print(i+1,product_list[shopping_list[i]]) auth = input(‘user or admin,please type "U" or A:‘) if auth == ‘U‘: user() elif auth == ‘A‘: admin_key = input(‘please input the key:‘) if admin_key == ‘admin‘: admin() else: exit()
第一个稍微有点完整的Python小程序。好多垃圾代码。没办法,零基础。
Mark 一下,文件操作部分有待加强。
with 结束后不能对文件操作。os.remove 和os.rename 提示出错。