購物車

isdgit() 判斷是否是數(shù)字

enumerate()是python的內(nèi)置函數(shù)
enumerate在字典上是枚舉、列舉的意思
對于一個可迭代的(iterable)/可遍歷的對象(如列表、字符串),enumerate將其組成一個索引序列,利用它可以同時獲得索引和值
enumerate多用于在for循環(huán)中得到計數(shù)

# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'Wang.KuiQiang'
__mtime__ = '2017/9/19'

用戶輸入金額,打印商品列表
根據(jù)編號購買商品
選擇后,檢測余額是否足夠,
可隨時退出q,退出打印購買的商品和余額


"""
product_list = [
    ('iphone', 8200),
    ('Macbook pro', 9288),
    ('ipad', 3200)
]
shopping_list = []
balance = input('how much money you left?:')
if balance.isdigit():
    balance = int(balance)
    while True:
        for index, item in enumerate(product_list):
            print(index, item)
        choice = input('choice the Id number in the product list:')
        if choice.isdigit():
            choice = int(choice)
            if choice >= 0 and choice < len(product_list):
                item = product_list[choice]
                if item[1] > balance:
                    print('you don\'t have enough money ')
                else:
                    balance = balance - item[1]
                    shopping_list.append(item)
                    print('you\'ve paied %s and left %s' % (item[1], balance))
            else:
                print('the code is inexistence ')
        elif choice == 'q':
            print("--------shopping list------")
            for p in shopping_list:
                print(p)
            print("Your current balance:", balance)
            exit()
        else:
            print("invalid option")

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容