Shopping Cart Procedure
product_list = [
(' Mac ', 9000),
(' Kindle ',
(' Tesla ', 900000),
(' Python book ',
(' bike ', '),
]
saving = input (' Please input your money: ')
Shopping_car = []
if Saving.isdigit (): #验证输入的是否为数字格式, if established then continue
saving = Int (saving)
While True:
#打印商品内容
# for I in Product_list:
# Print (Product_list.index (i), i)
# for I in Enumerate (product_list,1):
# Print (i)
For i,v in Enumerate (product_list,1):
print (i,v) #print (i, ' >>>> ', v)
choice = input (' Select purchase item number [exit: Q]: ') #引导用户选择商品
if Choice.isdigit ():
choice = Int (choice) #验证输入是否合法
#len (product_list) Get the list length
If choice > 0 and Choice <= len (product_list):
P_item = product_list[choice-1] #将用户选择商品通过choice取出来
if p_item[1] < saving:
Saving-= p_item[1]
shopping_car.append (P_item) #如果钱够, subtract the price of the commodity with the saving principal and add the item to the shopping cart
Else:
print (' Insufficient balance, remaining%s '%saving)
print (P_item)
Else:
print (' Code not present ')
elif Choice = = ' Q ':
print ('--------------you and purchase the following items--------------')
For i in Shopping_car:
print (i) #循环遍历购物车内的商品, the shopping cart is stocked with purchased goods
print (' You still have%s dollar money '%saving)
Break
Else:
print (' Invalid input ')
Python Learning: Shopping Cart Program