raygor
# 1,完成一个商城购物车的程序。
# 商品信息在文件存储的,存储形式:
# name price
# 电脑 1999
# 鼠标 10
# 游艇 20
# 美女 998
# .......
#
# 要求:
# 1,用户先给自己的账户充钱:比如先充3000元。
# 2,读取商品信息文件将文件中的数据转化成下面的格式:
# goods = [{"name": "电脑", "price": 1999},
# {"name": "鼠标", "price": 10},
# {"name": "游艇", "price": 20},
# {"name": "美女", "price": 998},
# ...... ]
#
# 3,页面显示 序号 + 商品名称 + 商品价格,如:
# 1 电脑 1999
# 2 鼠标 10
# …
# n 购物车结算
# q或者Q退出程序。
#
# 4,用户输入选择的商品序号,然后打印商品名称及商品价格,并将此商品,添加到购物车,用户还可继续添加商品。
# 5,如果用户输入的商品序号有误,则提示输入有误,并重新输入。
# 6,用户输入n为购物车结算,依次显示用户购物车里面的商品,数量及单价,若充值的钱数不足,则让用户删除某商品,直至可以购买,若充值的钱数充足,则可以直接购买。
# 7,用户输入Q或者q退出程序。
# 8,退出程序之后,依次显示用户购买的商品,数量,单价,以及此次共消费多少钱,账户余额多少,并将购买信息写入文件。
#
# 完成1-3要求为C。
# 完成1-4要求为 C+。
# 完成1-6要求为B。
# 完成全部要求并且没有BUG为A 或者A +。





# 创建一个存储全部商品的字典,利用global 关键字将goods设为全局变量
def get_merchandises():
    global goods
    goods = []
    with open(r\'merchandise_info\', encoding=\'utf-8\', mode=\'r\') as file_handler:
            for line in file_handler:
                info = {}
                temp_list = line.split(\' \')
                info.setdefault(\'name\', temp_list[0].strip())
                info.setdefault(\'price\', temp_list[1].strip())
                goods.append(info)
    return
# 显示所有商品的信息及提示信息
def display_merchandises():
    for i in range(len(goods)):
        print(str(i + 1) + \' \' + goods[i][\'name\'] + \' \' + goods[i][\'price\'])
    print(\'n 购物车结算\')
    print(\'q或者Q退出程序\')
# 删除购物车商品,传入参数为商品号,返回布尔值代表是否删除成功
def cancel(index):
    if index <= len(goods):
        print(str(index) + \' \' + goods[index - 1][\'name\'] + \' \' + goods[index - 1][\'price\'])
        print(\'----------------------------------------\')
        account[\'shopping_car\'].remove(index)
    else:
        return False
    return True
# 将商品添加到购物车,传入参数为商品号,返回布尔值代表是否添加成功
def select(index):
    if index <= len(goods):
        print(str(index) + \' \' + goods[index - 1][\'name\'] + \' \' + goods[index - 1][\'price\'])
        print(\'----------------------------------------\')
        account[\'shopping_car\'].append(index)
    else:
        return False
    return True
# 结账,结账是否成功会以布尔形式返回
def settle():
    total_money = 0
    for i in range(0, len(goods)):
        quantity = account[\'shopping_car\'].count(i+1)
        total_money += quantity * int(goods[i][\'price\'])
        if quantity:
            print(str(i + 1) + \' \' + goods[i][\'name\'] + \' \' + goods[i][\'price\'] + f\'*{quantity}\')
    print(\'----------------------------------------\')
    if total_money > account[\'balance\']:
        return False
    account[\'balance\'] -= total_money
    return True
# 打印清单,无返回值
def summary():
    total_money = 0
    with open(r\'history\', encoding=\'utf-8\', mode=\'a\') as file_handler:
        file_handler.write(\'\n\')
        for i in range(0, len(goods)):
            quantity = account[\'shopping_car\'].count(i+1)
            total_money += quantity * int(goods[i][\'price\'])
            if quantity:
                file_handler.write(str(i + 1) + \' \' + goods[i][\'name\'] + \' \' + goods[i][\'price\'] + f\'*{quantity}\' + \'\n\')
                # print(str(i + 1) + \' \' + goods[i][\'name\'] + \' \' + goods[i][\'price\'] + f\'*{quantity}\')
        file_handler.write(str(total_money) + \' \' + str(account[\'balance\']))
        print(str(total_money) + \' \' + str(account[\'balance\']))
        print(\'----------------------------------------\')

# 主程序
get_merchandises()  # 获得goods字典
deposit(int(input(\'how much do u wanna deposit?\n\')))  # 向账户存钱
display_merchandises()  # 显示商品界面
while 1:
    inp = input()  # 输入商品号或操作
    if inp.isdecimal() and int(inp) <= len(goods):  # 如果是数字且属于商品号,将该商品加入购物车
        select(int(inp))
    elif inp.upper() == \'Q\':  # 退出程序
        break
    elif inp == \'n\':  # 结账
        while 1:
            re = settle()  # 根据结账函数返回结果判断是否结账成功
            if re:
                summary()  # 成功打印清单,推出while循环
                break
            else:  # 未成功,需要拿出部分商品
                while 1:
                    index = input(\'which one u want cancel?\')
                    print(\'----------------------------------------\')
                    if cancel(int(index)):  # 如果取消商品合法,推出内部循环,重新判断是否结账成功
                        break
        break
    else:  # 输出字符不合法
        print(\'Illegal input\')
        display_merchandises()  # 重新显示初始界面
        print(\'----------------------------------------\')

分类:

技术点:

相关文章: