Case1-用list写shoppingcart

#define product list
product_list = [
('Python book',45),
('iphone',5800),
('iwatch', 1500),
("bike",850),
("luckiin coffee",25)
]
_shop =[]

#input customer salary
salary = input("Please input your Salary!》》》:")
if salary.isdigit():
_salary = int(salary)

while True:
for index,item in enumerate(product_list): # 输入正确数字salary,打印product list
#print(product_list.index(item),item)
print(index,item)
print("\t")
_choice = input("Please choice and Press 'q' to exit》》》:")

if _choice.isdigit():
_choice = int(_choice)

if _choice <len(product_list ) and _choice>=0: #判读list范围
p_item = product_list[_choice]
if p_item[1]<=_salary: #钱够
_shop.append(p_item) #添加记录到_shop 列表中
_salary -= p_item[1] #扣钱
print("Add %s in shopping cart! Your current balance is \033[31;1m%s\033[0m" %(p_item, _salary))
else:
print("Not Enough Money!!!")
else:
print("商品 %s 不存在!!" %(_choice))

elif _choice == 'q':
print("------shopping list---------")
for p in _shop:
print (_shop)
print("Exit!")
exit()
else:
print("Invalid number!")
break #注意break的位置

else:
print("Invalid Salary!!!")
salary = input("Please input your Salary!》》》:")



###目前还有一个问题就是错误按键就退出,没有给用户重新input的机会!



猜你喜欢

转载自www.cnblogs.com/ywyin/p/8989640.html