Python simple shopping cart

product_list =[ 
('huawei',3000),
('hongmiNote3',3000),
('sanxing',2600),
('ThinkPad870',15000),
('Iphone8',5600),
('HuaweiP20',3300)
]#Commodity list
shopping_list = []#Shopping list
while True:
salary = input('Input you salary:')
#Input salary if salary.isdigit(): #Determine whether the input salary is a number
salary = int(salary)
while True:
for index,item in enumerate(product_list): #Print the product list
print(index,item)
user_choice = input('What do you want to buy? (Please enter quit to exit):') #Enter the product serial number
if user_choice.isdigit( ):
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice > 0: #Determine whether the product serial number is in the product list
p_item = product_list[user_choice]
if p_item[1] <= salary: #Determine whether you can afford
shopping_list.append(p_item)
salary -= p_item[1]
print('Added %s into shopping car,your current balance is \033[31;1m%s\033[0m' %(p_item,salary))
else:
print("\033[41; 1m, your balance is only [%s]\033[0m" % salary)
else:
print('product_code [%s] is not excit'% user_choice)
elif user_choice == 'quit':
print('------shopping list-------')
for i in shopping_list:
print(i)
print('your current balance:%s' % salary)
exit()
else:
print('input true product_code,product_code is a number')
# break
else:
print('Please input correctly your salary')

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324929947&siteId=291194637