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")