Python---购物车更新程序 商品信息存在文件当中,用户购买记录要打印已购商品,余额。卖家可以添加商品,修改价格。

分为两部分:1.用户入口

commoditydata={"Iphone7plus":5000,
               "HuiWei10":4000,
               "Apple iwatch":2500,
               "Apple ipad":2000,
               "Ruishi watch":3500,
               "Ordinary world":78,
               "The dynasty of one":45,
               "milk tea":10,
               "pizza":55,
               "coffee":15,
               }
user_shopcar={}
salary=input("Please input your salary:")
if salary.isdigit():
    salary=int(salary)
    print(type(salary))
while True:
     print("-----The next time-----")
     for i in commoditydata:
         print(i,commoditydata[i])
     user_choice=input("please choose what you buy:")

     if user_choice in commoditydata:
        print(commoditydata[user_choice])
        if salary>=commoditydata[user_choice] :
             user_shopcar[user_choice]=commoditydata[user_choice]
             salary-=commoditydata[user_choice]
             print("\033[41;1m[%s] goods is in your shopcar,your balance is [%s]\033[0m"%(user_choice,salary))
             commoditydata.pop(user_choice)
             g = open("com", "w", encoding="utf-8")
             g.write(str(commoditydata))
            g.close()
        else:
             print("sorry,your money is not enough")
    elif user_choice=='E':
        print("-----Shopcar-----")
        for s in user_shopcar:
             print(s,user_shopcar[s])
         print("your balance is %s"%salary)
        exit()
    else:
        print("sorry,what you want to buy is not exist")

2.卖家入口

commoditydata={"Iphone7plus":5000,
               "HuiWei10":4000,
               "Apple iwatch":2500,
               "Apple ipad":2000,
               "Ruishi watch":3500,
               "Ordinary world":78,
               "The dynasty of one":45,
               "milk tea":10,
               "pizza":55,
               "coffee":15,
               }
# user_shopcar={}
# salary=input("Please input your salary:")
# if salary.isdigit():
#     salary=int(salary)
#     print(type(salary))
# while True:
#      print("-----The next time-----")
#      for i in commoditydata:
#          print(i,commoditydata[i])
#      user_choice=input("please choose what you buy:")
#
#      if user_choice in commoditydata:
#         print(commoditydata[user_choice])
#         if salary>=commoditydata[user_choice] :
#              user_shopcar[user_choice]=commoditydata[user_choice]
#              salary-=commoditydata[user_choice]
#              print("\033[41;1m[%s] goods is in your shopcar,your balance is [%s]\033[0m"%(user_choice,salary))
#              commoditydata.pop(user_choice)
#              g = open("com", "w", encoding="utf-8")
#              g.write(str(commoditydata))
#             g.close()
#         else:
#              print("sorry,your money is not enough")
#     elif user_choice=='E':
#         print("-----Shopcar-----")
#         for s in user_shopcar:
#              print(s,user_shopcar[s])
#          print("your balance is %s"%salary)
#         exit()
#     else:
#         print("sorry,what you want to buy is not exist")
seller_name=["xiaohua","xiaoming","xiaoli","xiaowang"]
seller=input("please input your name:")
if seller in seller_name:
    while True:
       seller_choice=input("Are you add good or modify price?'a'represent add ,'m'represent modify price,'e'represent exit:" )
       if seller_choice=='a':
           print("please start add:")
           add_good=input("input good you want to add:")
           if add_good not in commoditydata:
               add_goodprice=input("input good price:")
               if add_goodprice.isdigit():
                   add_goodprice=int(add_goodprice)
                   commoditydata[add_good]=add_goodprice
                   f=open("sell","w",encoding="utf-8")
                   f.write(str(commoditydata))
                   f.close
                   print("you have add %s successfuly" % add_good)
               else:
                   print("input wrong")
           else:
               print("sorry,what you add has existed")
       elif seller_choice=='m':
           print("please start modify:")
           modify_good = input("input good you want to modify:")
           if modify_good  in commoditydata:
              modify_goodprice = input("input good price:")
              if  modify_goodprice.isdigit():
                  modify_goodprice = int(modify_goodprice)
                  commoditydata[modify_good] = modify_goodprice
                  f = open("sell", "w", encoding="utf-8")
                  f.write(str(commoditydata))
                  f.close
                  print("you have modify %s successfuly"%modify_good)
              else:
                  print("input wrong")
           else:
               print("sorry,what you modify is not exist")
       elif seller_choice=="e":
            exit()
       else:
            print("choice is wrong")
else:
   print("sorry,you do not have authentication")
 
  

现在是单独的两个程序,可以将两个程序融合起来,用户走用户接口,卖家走卖家接口。

你可能感兴趣的:(python基础)