Python作業2:購物車優化

實現:

判斷用戶身份

(1)商家

商品信息存在文件“商品列表”裏,可以增加商品和修改商品價格

(2)顧客

1.打印商品列表(與商家所用列表同步),從文件中讀取餘額
2.允許用戶根據商品名稱購買商品
3.用戶選擇商品後,監測餘額是否夠,夠就直接扣款,不夠就提醒
4.已購商品與餘額保存至“購物清單”文件

#  -*- coding: utf-8 -*-
"""
@Author:yhf1995
@time:2020/2/2 20:00
"""
info={'1':'顧客', '2': '商家'}
for i in info:
    print(i,info[i])
choice=input("請選擇您的身份>>")

product_list = []  # 存儲商品名稱和價格的列表
if choice == '1':  # 顧客入口
    with open("product_list", "r", encoding="utf-8") as f:
        # 讀商品列表的文件,存儲格式是Iphone,8500,換行,然後下一個
        for line in f:
            (name, value) = line.strip().split(",")  # 按照“,”分片存儲爲一個元組
            product_list.append([name, int(value)])  # 添加到商品列表中

# 讀取現在擁有的金額
    with open("salary", "r", encoding="utf-8") as f_salary:
        for line in f_salary:
            salary = line

    shopping_list = []  # 存購買商品的列表
    if salary.isdigit():
        salary = int(salary)
    # 判斷輸入的金額是否爲數字,如果是將其轉爲int類型,如果不是,輸出 輸入有誤
        while True:
            for item in product_list:
                print(product_list.index(item), item)
                # 打印商品列表
            choice_number = input("Please enter the number of the product you want:>>>")
            if choice_number.isdigit():
                choice_number = int(choice_number)
                # 判斷輸入的商品編號是否爲數字,如果是轉爲int型,如果不是,提示輸入有誤
                if 0 <= choice_number < len(product_list):
                    # 判斷商品是否存在
                    if salary >= product_list[choice_number][1]:
                        shopping_list.append(product_list[choice_number])
                        salary -= product_list[choice_number][1]
                        print("add shop %s in your shopping cart,and your salary is \033[31;1m%s\033[0m!"%(product_list[choice_number],salary))
                        # 判斷餘額是否足夠,餘額足夠,將商品放入購物車,餘額減去該商品價格,輸出放入購物車的商品和餘額
                    else:
                        print("You have insufficient balance!")
                        # 餘額不足
                else:
                    print("product_number does not exist!")
                    # 商品不存在
            elif choice_number == "Q":
                print("The shopping cart list is as follows:")
                # 打印購買商品列表
                for shop_item in shopping_list:
                    print(shop_item)
                # 打印剩餘金額
                print("Your current balance is %s." % ( salary ))
                #  更新以購買商品文件
                with open("shopping_list","a",encoding="utf-8") as f_shop_cart:
                    f_shop_cart.write("\n")
                    for shop_item in shopping_list:
                        f_shop_cart.write(shop_item[0])
                        f_shop_cart.write('\t')
                        f_shop_cart.write(str(shop_item[1]))
                        f_shop_cart.write("\n")
                # 更新以剩餘金額文件
                with open("salary","w",encoding="utf-8") as f_salary:
                    f_salary.write(str(salary))
                exit()
                # 輸入Q 打印購物車商品和餘額,並退出購物車程序
            else:
                print("The product_number is error!")
                # 商品編號有誤
    else:
        print("The amount entered is not a number!")
        # 金額有誤
elif choice == '2':
    with open("product_list", "r", encoding="utf-8") as f:
        for line in f:
            (name, value) = line.strip().split(",")
            product_list.append([name, int(value)])
    for item in product_list:
        print(product_list.index(item), item)
        # 打印商品列表
    choice2 = input("請輸入操作代號,U表示修改商品價格,A表示增加商品:")

    if choice2 == "U":
        while True:
            choice_update_number = input("Please enter the number of the product you want update:>>>")
            if choice_update_number.isdigit():
                choice_update_number = int(choice_update_number)
                # 判斷輸入的商品編號是否爲數字,如果是轉爲int型,如果不是,提示輸入有誤
                if 0 <= choice_update_number < len(product_list):
                    # 判斷商品是否存在
                    update_value = input("請輸入修改後的商品價格:")
                    if update_value.isdigit():
                        product_list[choice_update_number][1] = update_value
                    else:
                        print("輸入的價格不是數字!")
                else:
                    print("product_number does not exist!")
                # 商品不存在
            elif choice_update_number == "Q":
                with open("product_list", "w", encoding="utf-8") as f:
                    for shop_item in product_list:
                        f.write(shop_item[0])
                        f.write(',')
                        f.write(str(shop_item[1]))
                        f.write("\n")
                exit()
                # 輸入Q 打印購物車商品和餘額,並退出購物車程序
            else:
                print("輸入的內容有誤,請輸入商品序號,退出請輸入Q!")
    elif choice2 == "A":
        while True:
            shop_name = input("請輸入要增加的商品名稱,或者輸入Q退出:")
            if shop_name != "Q":
                shop_value = input("請輸入要增加的商品價格:")
                product_list.append([shop_name,shop_value])
            else:
                with open("product_list", "w", encoding="utf-8") as f:
                    for shop_item in product_list:
                        f.write(shop_item[0])
                        f.write(',')
                        f.write(str(shop_item[1]))
                        f.write("\n")
                exit()
    else:
        print("您輸入的操作代號有誤!")
    # 商家需要登錄#
else:
    print("您輸入的身份代碼有誤!")

 所需的文件有product_list,shoping_list 和salary

Product_list裏的內容格式如下:

Iphone,8500
Mac Pro,11200
Starebuck Latte,31
Alex python,81
Bike,800
motobike,5000

shoping_list裏面最初是空的

salary裏面 就只有一個數字

本程序實現了基本功能,本來還想在商家的那部分加入上一個作業的登陸接口,登陸成功以後才能進行商家操作,但發現都是一個文件讀取的套路,就沒加,主要還是懶,哈哈哈

題目來源:oldboy教育python課程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章