Week2作业:购物车

#coding=utf-8
#Version:python3.6.0
#Tools:Pycharm 2017.3.2
__date__ = '2018/9/17 19:00'
__author__ = 'Steven'

'''
商家入口:
1.可添加商品,修改商品价格
'''
shop_shelf = {}
print("The commodity list on your shelf is as follow:")
with open('shop_shelf.txt', 'r', encoding='UTF-8') as shop_shelf_file:
    for line in shop_shelf_file:
        print(line.strip("\n"))
        split_line = line.split()
        shop_shelf[split_line[0]] = int(split_line[1])


user_action = str(input("Please input your action to manage the commodity>>> (Such as: A XX 123; D XX; M XX 321;)"))
action = user_action.split(" ")
if ("D" == action[0] or "M" == action[0]) and action[1] not in shop_shelf:
    print("Error: object not existed. Will be automatically added before modifying it")

if "A" == action[0] or "M" == action[0]:
    print(action[1], " Added" if (("A" == action[0]) or (action[1] not in shop_shelf)) else " Modified")
    shop_shelf[action[1]] = int(action[2])
elif "D" == action[0]:
    shop_shelf.pop(action[1])
    print(action[1], " Deleted")
else:
    print("Invalid input")

try:
    with open('shop_shelf.txt', 'w') as shop_shelf_file:
        for key in shop_shelf:
            temp_lines = [key, ' ', str(shop_shelf[key]),'\n']
            shop_shelf_file.writelines(temp_lines)
        shop_shelf_file.close()
except:
    print("Fator Error: File writing failed! ")
Iphone 12999
Huawei 3899
Xiaomi 3108
Hongmi 799
Hornor 1999
#coding=utf-8
#Version:python3.6.0
#Tools:Pycharm 2017.3.2
__date__ = '2018/9/17 18:57'
__author__ = 'Steven'

'''
用户入口:
1.商品信息存在文件里
2.已购商品,余额记录
'''
'''读取账户余额和已买到的商品'''
shopping_cart = {}
with open('shopping_cart.txt', 'r', encoding='UTF-8') as shopping_cart_file:
    for line in shopping_cart_file:
        split_line = line.split()
        shopping_cart[split_line[0]] = [int(split_line[1]),int(split_line[2])]
if "balance" in shopping_cart:
    print("Dear customer, your account balance is now ", shopping_cart["balance"][0])
else:
    print("Shopping cart file damaged: No balance record")
    exit(1)

'''低调读取货架上商品列表'''
shop_shelf = {}
with open('shop_shelf.txt', 'r', encoding='UTF-8') as shop_shelf_file:
    for line in shop_shelf_file:
        split_line = line.split()
        shop_shelf[split_line[0]] = int(split_line[1])

salary = input("Please input the new income >>> (Press q to skip)")
if "q" == salary:
    pass
elif salary.isdigit():
    salary = int(salary)
    shopping_cart["balance"][0] += salary
    shopping_cart["balance"][1] = salary
    while True:
        print("\nPlease choose your goods:")
        for commodity in shop_shelf:
            print(commodity,"", shop_shelf[commodity])
        choice = input("Choose>>> (Current balance {bls}, Press q to quit)".format(bls = shopping_cart["balance"][0]))
        if "q" == choice:
            break
        elif choice in shop_shelf:
            if shopping_cart["balance"][0] >= shop_shelf[choice]:
                shopping_cart["balance"][0] -= shop_shelf[choice]
                if choice in shopping_cart:
                    shopping_cart[choice][1] += 1
                else:
                    shopping_cart[choice] = [shop_shelf[choice], 1]
            else:
                print("Your balance {bls} is insufficient for {ch}".format(bls = shopping_cart["balance"][0], ch = choice))
        else:
            print("Error: invalid input(commodity name)")
else:
    print("Error: invalid input(income)")

print("Now your account and shopping cart status:")
try:
    with open('shopping_cart.txt', 'w') as shopping_cart_file:
        for key in shopping_cart:
            print(key, ' ', str(shopping_cart[key][0]),' ', str(shopping_cart[key][1]))
            temp_lines = [key, ' ', str(shopping_cart[key][0]),' ', str(shopping_cart[key][1]), '\n']
            shopping_cart_file.writelines(temp_lines)
        shopping_cart_file.close()
except:
    print("Fator Error: File writing failed! ")
balance 2096 3000
Iphone 12999 1
Huawei 3899 2
Xiaomi 3108 1
Hornor 1999 1

猜你喜欢

转载自www.cnblogs.com/InsaneSteven/p/9665705.html