【Python 学习】 购物车实例

Life is short, I learn Python

#!/bin/bash/env python 
# -*- coding: utf-8 -*-
# Author:Meleuo
# Blog : zheli.ink

commodity = [
    (5000,"iPhone"),
    (10000,"Mac Pro"),
    (1000,"Ari dost"),
    (3000,"MI 9"),
    (2000,"Redmi k20")
]
shopping_list = []
wages = input("How much is your salary? :")
wages2 = wages
if wages.isdigit():
    wages = int(wages)
    wages2 = wages
    while True:
        for index,comm_list in enumerate(commodity):
            print(index,comm_list)
        purchase = input("You want to buy that? :")
        if purchase.isdigit():
            purchase = int(purchase)
            if purchase < len(commodity) and  purchase >= 0:
                purchase_ = commodity[purchase][0]
#                print(commodity[purchase][0])
#                print(wages)
                if wages >= purchase_:
                    balance = (wages - purchase_)
                    wages = balance
                    print("Congratulations on your success in purchasing",commodity[purchase][1],"with a balance of",balance)
                    shopping_list.append(commodity[purchase])
                    wages3 = (wages2 - wages)
                else:
                    print("Sorry, you don't have enough money.")
        elif purchase == "q":
                wages3 = (wages2 - wages)
                print("You have purchased",shopping_list," of these goods, totaling ",wages3,", yuan. Your balance is",wages)
                exit()
        else:
            print("Your input is incorrect")
else:
    print("Your input is incorrect")
Last modification:May 30th, 2019 at 05:33 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment