国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現購物系統(示例講解)

2020-02-16 10:14:25
字體:
來源:轉載
供稿:網友

要求:

用戶入口

1、商品信息存在文件里
2、已購商品,余額記錄。

商家入口

可以添加商品,修改商品價格

Code:

商家入口:

# Author:P J Jimport osps = '''1 >>>>>> 修改商品2 >>>>>> 添加商品按q為退出程序'''# 打開兩個文件,f文件為原來存取商品文件,f_new文件為修改后的商品文件f = open('commodit', 'r', encoding='utf-8')f_new = open('commodit_update', 'w+', encoding='utf-8')file_list = f.readlines()# 打印商品信息while True: productslist = [] # 從商品文件中讀取出來的數據存放到productslist列表里 for line in file_list:  productname = line.strip().split()  productname, oldprice = line.strip("/n").split()  productslist.append([productname, int(oldprice)]) choose = input("%s請選擇:" %ps) if choose =='1':  for index, item in enumerate(productslist):   print(index, item)  productindex = input("請輸入要修改價格的商品序號:")  if productindex.isdigit():   productindex = int(productindex)  while True:   print('要修改商品信息:', productslist[productindex])   price = input("請輸入要修改的價格:")   if price.isdigit():    price = int(price)    productslist[productindex][1]=price    break   else:    print("請正確的輸入價格!")    continue  #已經修改好的商品列表循環寫入f_new文件夾  for products in productslist:   insert_data = "%s %s" %(products[0],products[1])   f_new.write(insert_data+'/n')  print("商品價格已經修改!")  # 替換原來的文件  f_new = open('commodit_update', 'r', encoding='utf-8')  data = f_new.readlines()  f = open('commodit', 'w+', encoding='utf-8')  for line in data:   f.write(line)  f.close()  f_new.close()  #刪除替換文件  os.remove('commodit_update') elif choose =='2':  # 添加商品  f = open('commodit', 'a+', encoding='utf-8')  pricename = input("請輸入商品名:")  while True:   price = input("請輸入商品價格:")   if price.isdigit():    f.writelines('%s %s/n' % (pricename, price))    break   else:    print('輸入錯誤請重新輸入!')    continue  f.close()  continue elif choose =='q':  break else:  print("輸入錯誤請重新輸入")  continue

買家入口:

# Author:P J Jproductslist = []f = open('commodit','r',encoding='utf-8')for line in f: productname,price = line.strip('/n').split() productslist.append((productname,int(price)))print(productslist)shopping_list = []salary = input("請輸入你的現金:")if salary.isdigit(): salary = int(salary) while True:  # for item in productslist:  #  print(productslist.index(item),item)  for index,item in enumerate(productslist):   print(index,item)  #判斷用戶要輸入  user_choice = input("請選擇要買什啥>>>:")  if user_choice.isdigit():   user_choice = int(user_choice)   if user_choice < len(productslist) and user_choice >= 0:    p_item = productslist[user_choice]    if p_item[1] <= salary: #買得起     shopping_list.append(p_item)     salary -=p_item[1]     print("加入 %s 購物車你的余額是/033[31;1m%s/033[0mRMB" %(p_item,salary))    else:     print("/033[32;1m 你的余額只剩[%s]RMB啦,還買個毛線/033[0m " %salary)   else:    print("/033[41;1m您輸入的商品不存在,請重新輸入!/033[0m")  elif user_choice == 'q':   print("----shopping_list----")   for p in shopping_list:    print(p)   print("你的余額:/033[31;1m%s/033[0mRMB" %salary)   #簡單的余額記錄   f = open('salary','w+',encoding='utf-8')   f.writelines(str(salary))   f.close   exit()  else:   print("錯誤選項")            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 应城市| 山东| 始兴县| 孝感市| 台安县| 贵定县| 新蔡县| 宁国市| 尼木县| 大余县| 平凉市| 旅游| 调兵山市| 乐亭县| 林州市| 汤阴县| 集安市| 礼泉县| 宝坻区| 吉首市| 灵山县| 铁岭市| 峨眉山市| 睢宁县| 惠东县| 泽普县| 漳州市| 聂荣县| 白水县| 浑源县| 庆元县| 海林市| 莱州市| 嘉峪关市| 武威市| 巨野县| 澄城县| 望都县| 阿拉善左旗| 奉新县| 荔波县|