购物车程序流程图:
购物车程序:
product_list=[('iphone',5800),('watch',12000),('bike',800),('book',120),('coffee',31), ('tea',50)] shopping_list=[] while True: salary=input("please input your salary:") if salary.isdigit(): salary=int(salary) while True: for index,item in enumerate(product_list): print(index,item) choose=input("now you can choose your wanting!") if choose.isdigit(): choose=int(choose) if choose<len(product_list) and choose>=0: p_item=product_list[choose] if salary>=p_item[1]: shopping_list.append(p_item) salary-=p_item[1] print("added %s in your shopping cart and now your balance is %s"%(p_item,salary)) else: print("your balance is not enough! now your balance is %s"%salary) else: print("your choice is wrong!") elif choose=='q': print("looking forward your next coming!\n" "-------shoppping list------\n" "now your balance is%s"%salary) for p_item in shopping_list: print(p_item) exit() else: print("your choose is wrong!you can choose others!") else: print("your salary is wrong! you can input it again!")
小结:
一、“
for index,item
in
enumerate(product_list):”enumerate--取出商品下标。
二、“
\033
[31;1m%s
\033
[0m ”:加色强调。31--红色;32--绿色;41--红底;42--绿。
三、exit()退出
四、“.isdigit()”判断是否为数字。