购物车程序

需求:

  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

  

转载于:https://www.cnblogs.com/freelandun/p/6770315.html

你可能感兴趣的:(购物车程序)