标签:art [] pen int choice 循环 app car 选择
product_list = [
(‘iphone‘,8000),
(‘Mac Pro‘,9800),
("Bike",3000),
("Watch",10000),
("Cofee",30),
("Book",100),
] #定义商品列表
shoppint_list = [] #购物车空列表
while True:
salary = input("Input you salary:") #输入预算
if salary.isdigit(): #isdigit 整数
salary = int (salary)
while True: #循环
# for item in product_list:
# print(product_list.index(item),item)
for index,item in enumerate(product_list): #下标做产品编号、 enumerate取出列表下标
print(index,item) #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 :
shoppint_list.append(p_item)
salary -= p_item[1]
print("Added %s into shopping cart,your current balance is %s" %(p_item,salary) )
else:
print("\033[41;1m你的余额只剩[%s]了,还买个鸡腿\033[0m" %(salary)) # "\033[[\033[Om" 加亮色
else:
print("商品不存在")
elif user_choice == ‘q‘:
print("----商品列表------")
for p in shoppint_list:
print(p)
print("you current balance:",salary)
exit()
else:
print("invalid option")
else:
print("请输入数字")
标签:art [] pen int choice 循环 app car 选择
原文地址:https://www.cnblogs.com/huangxd/p/8849682.html