1. 程式人生 > >shopping cart

shopping cart

art ren isdigit dde valid () lse end add

product_list = [
(‘phone1‘,5000),
(‘phone2‘,4000),
(‘phone3‘,3000),
(‘phone4‘,2000),
(‘phone5‘,1000),
(‘phone6‘,100),
]
shopping_list = []
salary = input("your salary:")
if salary.isdigit():
salary = int(salary)
while True:
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 >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary:
shopping_list.append(p_item)
salary -= p_item[1]
print("Added %s to shopping cart,your current balance is %s"%(p_item,salary))
else:
print("only [%s],not enough "%salary)
else:
print("product code [%s] is not exist!"%user_choice)
elif user_choice == ‘q‘:
print("-------shopping_list-------")
for p in shopping_list:
print(p)
print("Your current balance is ",salary)
exit()
else:
print("Invalid option")

shopping cart