1. 程式人生 > >python簡單的購物車程式(含程式碼)

python簡單的購物車程式(含程式碼)

效果圖:

'''
Created on 2017年9月4日

@author: len
'''


product_list = [
    ('Robot',200000),
    ('MacPro',12000),
    ('Iphone8',8888),
    ('Hello World',1200),
                ] 
shopping_list = []
user_salary=input("請輸入你的工資:")
if user_salary.isdigit():
    user_salary = int(user_salary)
    while True:
        print("商品如下:")
        for index,item in enumerate(product_list): 
            
            print (index,item)
        user_choice = input("請輸入要購買的商品編號:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(product_list) and user_choice > -1:
                p_item = product_list[user_choice]
                if user_salary>=p_item[1]:
                    shopping_list.append(p_item)
                    user_salary-=p_item[1]
                    print("購買商品",p_item,"成功您的餘額為",user_salary,"元!" )
                else:
                    print("您的餘額為",user_salary,"餘額不足以購買此商品,購買失敗!")
                            
            else:
                print("並無此產品!")
        elif user_choice == "q":
            print("--------shopping list-------")
            for i in shopping_list:
                print(i)
            exit()
        else:
            print("invalidate!!!")