product_list = [("mac",9000),
("bike",1500),
("book",80),
("tesla",900000),
("kindle",800)] #此处需注意:元组的书写方式,如果写成了"mac,9000",则9000的格式为string且无法比较
salary = input("please input your salary:")
shopping_car = []
if salary.isdigit():
salary = int(salary)
while True:
for i,v in enumerate(product_list,1):#enumerate每次回返回一个tuple:(index, value)
print(i, ">>>",v)
# while True: # 如果条件判断语句永远为 true,循环将会无限的执行下去,此处表示可以循环选择
choice =input("please enter your choice[q:quit]:")#如果将choice 缩进了,一次只得到一个输出1 >>> mac,9000
# 2>>>bike,1500 3>>>book,80,因为 enumerate 函数表示遍历该列表,如果缩进相当于 i=1,选择一次;i=2,选择一次
#i =3,又选择一次,循环的层次不对
if choice.isdigit():
choice = int(choice)
if choice >0 and choice<= len(product_list) :#为什么用for choice >0 and choice<= len(product_list)报错,can't assign to operator
p_item = product_list[choice-1]
if p_item[1] < salary:
salary -= p_item[1]
shopping_car.append(p_item)
else:
print("余额不足")
else:
print("选择不在商品列表中")
elif choice == "q":
print("退出,您已购买如下商品")
for a in shopping_car:#遍历
print(a)
# print(shopping_car)
print("余额%s"%salary)
break
else:
print("请输入数字")
else:
print("please input digits.")
转载于:https://www.cnblogs.com/ljk21313/p/6958953.html