购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额
salary = int(input("Input your salary: "))
goods = ['iphone', 'book', 'desk', 'cup']
price = [5800, 20, 55, 35]
for i in range(4):
	print(i+1, '--', goods[i], '--', price[i])
cost = 0
cart = []
while True:
	buy = input("Which would you want? ")
	if buy.isdigit():
		buyid = int(buy)
		if buyid >= 1 and buyid <= 4:
			cost += price[buyid-1]
			if cost > salary:
				print("No stufficient funds. Try again.")
				cost -= price[buyid-1]
				continue
			cart.append(goods[buyid-1])
			print("  Your cost is ", cost, ", Your balance is ", salary-cost)
		else:
			print("Input between 1 and 4.")
			continue
	elif buy == 'q':
		for i in cart:
			print(i, )
		print("  Your cost is ", cost, ", Your balance is ", salary-cost)
		break
	else:
		print("Input between 1 and 4, or q(quit).")
		continue

  

posted @ 2017-04-26 19:25  云中王  阅读(190)  评论(0编辑  收藏  举报