zoukankan      html  css  js  c++  java
  • 购物车程序

    需求:

    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
    

      

  • 相关阅读:
    UML_04_时序图
    UML_03_类图
    UML_02_概述
    UML_00_资源帖
    UML_01_画图工具
    SpringCloud_00_资源帖
    Idea_03_常用快捷键
    Idea_02_常用配置
    Idea_01_安装与激活
    Codeforces命令行工具
  • 原文地址:https://www.cnblogs.com/freelandun/p/6770315.html
Copyright © 2011-2022 走看看