1. 程式人生 > >Python程序編寫購物小程序

Python程序編寫購物小程序

put_user not 啟動程序 開始 要求 商品 use value 三次

購物車要求:

       用戶名和密碼存放於文件中
       啟動程序後,先登錄,登錄成功則讓用戶輸入工資,然後打印商品列表,失敗則重新登錄,超過三次則退出程序
       允許用戶根據商品編號購買商品
       用戶選擇商品後,檢測余額是否夠,夠就直接扣款,不夠就提醒
       可隨時退出,退出時,打印已購買商品和余額
#!/usr/bin/env python
# -*- coding: utf-8 -*-

‘‘‘
用戶名和密碼存放於文件中
啟動程序後,先登錄,登錄成功則讓用戶輸入工資,然後打印商品列表,失敗則重新登錄,超過三次則退出程序
允許用戶根據商品編號購買商品
用戶選擇商品後,檢測余額是否夠,夠就直接扣款,不夠就提醒
可隨時退出,退出時,打印已購買商品和余額
‘‘‘

product_dic = { 1:[‘Iphone7‘,5800],
                2:[‘Coffee‘,30],
                3:[‘疙瘩湯‘,10],
                4:[‘Python Book‘,99],
                5:[‘Bike‘,199],
                6:[‘ViVo X9‘,2499],
}

shop_list=[]
shop_dic={}
def shop():
    Tag=True
    remain_Balance = Balance
    print("開始購物".center(30,"="))
    for key,value in product_dic.items():
        print(key,value)
    while Tag:
        serial_number=input("輸入你要購買商品的編號>>:").strip()
        if serial_number.isdigit():
            serial_number = int(serial_number)
            if serial_number > 6:
                print("請輸入1-6")
                continue
        elif serial_number == "q":
            print("開始結算".center(20,"="))
            for list in shop_list:
                product_name = list[0]
                product_price = list[1]
                shop_dic.setdefault(product_name, {})
                shop_dic[product_name].setdefault("number", 0)
                shop_dic[product_name].setdefault("total", 0)
                shop_dic[product_name]["price"] = product_price
                if product_name in shop_dic:
                    shop_dic[product_name]["number"] += 1
                shop_dic[product_name]["total"] = shop_dic[product_name]["price"] * shop_dic[product_name]["number"]
            # 總價
            total = 0
            for product in shop_dic:
                print(product.center(7),str(shop_dic[product]["number"]).center(7),str(shop_dic[product]["total"]).center(5))
                total = total + shop_dic[product]["total"]
            # if total > Balance:
            #     print("余額不足購買這麽多")
            break
        elif serial_number == "exit":
            print("直接退出")
        else:
            continue
        print("你要購買的商品編號{},商品{},價格{}".format(serial_number,product_dic.get(serial_number)[0],product_dic.get(serial_number)[1]))
        yes_no=input("輸入y/n,確定加入購物車>>:")
        if yes_no == "y" :
            if product_dic.get(serial_number)[1] > remain_Balance:
                print("您的余額不夠.無法加入到購物車,還差{}".format(product_dic.get(serial_number)[1]-remain_Balance))
            else:
                shop_list.append(product_dic.get(serial_number))
                remain_Balance = remain_Balance - product_dic.get(serial_number)[1]
                print(shop_list,remain_Balance)
            continue
        elif yes_no == "n":
            print(shop_list)
            pass
            continue
        elif yes_no == "exit":
            break
        else:
            print("非法輸入,請輸入y或者n")
            continue

user_info={}
while True:
    print(‘‘‘購物車小程序:
    1、購物
    2、註冊賬號
    輸入q退出
    ‘‘‘)
    option=input("your option>>:").strip()
    if not option.isdigit():
        print("input 1 or 2\n")
        continue
    option=int(option)
    if option > 2:
        print("input 1 or 2\n")
        continue

    if option == 1:
        Tag=True
        count=0
        while Tag:
            input_user = input("your name>>:").strip()
            with open("user.txt", encoding="utf-8", mode="r") as read_f:
                for line in read_f:
                    line=line.strip("\n")
                    User=line.split(",")[0]
                    Password=line.split(",")[1]
                    Balance=line.split(",")[2]
                    if input_user == User:
                        user_info.setdefault(User,{})
                        user_info[User].setdefault("count",0)
                        user_info[User]["Password"]=Password
                        user_info[User]["Balance"]=Balance
                        if user_info[User]["count"] >= 3:
                            print("%s用戶鎖定" %(input_user))
                            Tag=False
                        break
                else:
                    print("{}用戶不存在".format(input_user))
                    continue
                if Tag:
                    input_password = input("your password>>:")
                    if input_password == user_info[input_user]["Password"]:
                        print("{}用戶密碼登陸正確".format(input_user))
                        print("用戶:{},余額:{}".format(User, user_info[User]["Balance"]))
                        ‘‘‘
                        這時候才開始購物
                        ‘‘‘
                        Balance=int(Balance)
                        shop()
                        break
                    else:
                        user_info[input_user]["count"]+=1
                        print("{}用戶密碼登陸錯誤,還有{}嘗試機會".format(input_user, 3 - user_info[input_user]["count"]))

  

Python程序編寫購物小程序