
product_list = [ ("电脑",5800), ("phone",4200), ("pen",66), ("book",99), ("watch",10800), ] salary = input("输入工资:") shopping_list = [] buy_money = 0 if salary.isdigit(): salary = int(salary) while True: for index,value in enumerate(product_list): print(index,value) choice = input("输入产品编号:") if choice.isdigit(): choice = int(choice) if choice >= 0 and choice < len(product_list): item = product_list[choice] if salary >= item[1]: salary -= item[1] buy_money += item[1] shopping_list.append(item) for i in shopping_list: print (i) print("你当前的余额是%d" % salary) else: print("你当前的余额是%d,可用余额不足" % salary) else: print("没有此商品") elif choice == 'q': print("shopping list".center(50,'-')) for i in shopping_list: print(i) print("你购物消费%d元,当前的余额是%d" %(buy_money,salary)) break else: print("输入'q'或者编号") else: print("输入错误")
isdigit 判断输入是否由数字组成
enumerate print 把列表元素的编号和元素打印出来
append 添加元素方式
count = 5 while count >= 0: print("count:%d" % count) count -= 1 else: print("跳出循环后打印count: %d" % count)
结果:
count:5
count:4
count:3
count:2
count:1
count:0
跳出循环后打印count: -1