Python模块列表字典(3)

while not exit_flag:
    for i in data:
        print(i)
    choice = input("选择进入1>>:")
    if choice in data:
        while not exit_flag:
            for i2 in data[choice]:
                print("\t",i2)
            choice2 = input("选择进入2>>:")
            if choice2 in data[choice]:
                while not exit_flag:
                    for i3 in data[choice][choice2]:
                        print("\t", i3)
                    choice3 = input("选择进入3>>:")
                    if choice3 in data[choice][choice2]:
                        for i4 in data[choice][choice2][choice3]:
                            print("\t\t",i4)
                        choice4 = input("最后一层,按b返回>>:")
                        if choice4 == "b":
                            pass
                        elif choice4 == "q":
                            exit_flag = True
                    if choice3 == "b":
                        break
                    elif choice3 == "q":
                        exit_flag = True
            if choice2 == "b":
                break
            elif choice2 == "q":
                exit_flag = True

十一、作业

购物车
用户入口:
1、商品信息存在文件里
2、已购商品,余额记录

商家入口:
2、可以添加商品,修改商品价格

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 用户或者是商家
# 用户-登陆信息-卡里剩多少钱-打开商品列表-选择商品-打印已购商品,余额记录
# 商家-登陆信息-添加商品/修改商品价格-存入文件
user_info = {"zhangsan":[333333,30000],"lisi":[444444,40000],"wangwu":[555555,50000]}
shopper_info = {"hhh":666666}
user = input("user:")
password = input("password:")
shopping_list = []
product_list = open("product_list.txt").readlines()
if user in user_info and int(user_info[user][0]) == int(password):
    salary = int(user_info[user][1])
    print("Welcome %s to our shopping center!Your salary is %s" % (user, salary))
    while True:
        for k, v in enumerate(product_list):
            print(k, v)
        choice = input("Which product do you want to select?")
        shopping_list.append(product_list[int(choice)])
        shop_price = int(product_list[int(choice)].split(":")[1])
        salary -= shop_price
        print("Your shopping_list is %s and your salary is %s" % (shopping_list, salary))
        choice2 = input("Do you want to continue to shopping?")
        if choice2 == "n":
            print("Welcome to you next time!Your shopping_list is %s and your salary is %s" % (shopping_list, salary))
            break
        elif choice2 == "y":
            pass
elif user in shopper_info and int(shopper_info[user]) == int(password):
    while True:
        print("1 add product\n""2 modify price")
        choice3 = input("What do you want to do?")
        if choice3 == "1":
            product = input("Which product do you want to add?")
            price = input("This product's price is?")
            product_info = "\n"+product+":"+price
            with open("product_list.txt",'a+') as f:
                f.write(product_info+'\n')
            choice4 = input("Do you want to continue?")
            if choice4 == "y":
                pass
            elif choice4 == "n":
                break
        elif choice3 == "2":
            for k, v in enumerate(product_list):
                print(k, v)
            choice5 = input("Which product do you want to modify?")
            new_price = input("This product's price is modify to?")
            old_price = product_list[int(choice5)].split(":")[1]
            file_data = ""
            with open("product_list.txt","r") as f:
                for line in f:
                    if old_price in line:
                        line = line.replace(old_price,new_price) + '\n'
                    file_data += line
            with open("product_list.txt","w") as f:
                    f.write(file_data)
            choice6 = input("Do you want to continue?")
            if choice6 == "y":
                pass
            elif choice6 == "n":
                break

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/fd1240a6d9a745963f6151a3edf48166.html