#author:leon
product_list= [
('iphone',5800),
('mac pro',9800),
('bike',800),
('watch',6000),
('coffee',31),
('book',120)
]
shopping_list= []
salary = input("input your salary:")
if salary.isdigit():
salary=int(salary)
while True:
for item in product_list:
print(product_list.index(item),item) #循环打印产品编号和产品列表(产品名加产品价格)
user_choice = input("选择买什么>>>>:")
if user_choice.isdigit(): #如果是数字成立,
user_choice=int(user_choice) #把数字转换成整型int
if user_choice<len(product_list) and user_choice>=0: #输入的数字长度必须小于产品列表的长度,#len()取列表长度
P_item=product_list[user_choice] #找出数字user_choice代表的值。例:name[1]找出产品
if P_item[1] <= salary: #买得起。列表product_list中嵌套了P_item元组。所以P_item[1],表示取出元祖中第二个值,即价格。
shopping_list.append(P_item)
salary -=P_item[1]
print("add %s into your shopping cart ,you current balance is \033[31;1m%s \033[0m"%(P_item,salary))
else:
print("\033[32;1m 你的余额不足 %s"%salary) elif user_choice =="q":
print(".....shopping list ...")
for p in shopping_list: #循环打印列表
print(p)
print("your balance %s " %salary)
exit()

cart_购物车小程序的更多相关文章

  1. python 购物车小程序

    python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...

  2. [作业] Python入门基础---购物车小程序

    1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的 ...

  3. python3 购物车小程序,余额写入文件保存

    python3 购物车小程序,余额写入文件保存 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan goods = ( ...

  4. Day2:购物车小程序

    一.购物车小程序第一版 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan product_list = [ (&quo ...

  5. python编写购物车小程序

     #练习#程序购物车#启动程序后,让用户输入工资,  然后打印商品列表,允许用户根据商品编号购买商品用户选择商品后 #检测余额是否够,够就直接扣款,不够就提醒可随时退出,退出时,打印已购买商品和余额  ...

  6. Python之路 day2 购物车小程序1

    #Author:ersa ''' 程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时 ...

  7. python写购物车小程序

    #!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Author: Skyell Wang # @Time : 2018/5/22 15:50 # 基础要 ...

  8. python 练习购物车小程序

    # -*- coding:utf-8 -*- shp = [ ['iphone',5000], ['offee',35], ['shoes',800] ] pric_list = [] e = int ...

  9. 购物车小程序(while循环,列表)

    while True: salary = input("please input your salary:") if salary.isdigit(): salary=int (s ...

随机推荐

  1. php 相同的产品 一个背景色

    Array ( [0] => 12 [1] => 17 [2] => 17 [3] => 17 [4] => 17 [5] => 3 [6] => 3 [7] ...

  2. 第一百五十八节,封装库--JavaScript,ajax说明

    封装库--JavaScript,ajax说明 封装库ajax()方法,ajax通讯方法,跨页面向动态页面发送或获取数据 /** ajax()方法,ajax通讯方法,跨页面向动态页面发送或获取数据 * ...

  3. 【BZOJ】2017: [Usaco2009 Nov]硬币游戏(dp+神题+博弈论)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2017 这题太神了,我想了一个中午啊 原来是看错题一直没理解题解说的,一直以为题解是错的QAQ “开始 ...

  4. 设置grid高度

    $("#jqxSalaryCalculation").jqxGrid({ height: $("#jqxTree").height() - 73 });

  5. Yii中的criteria 类

    $criteria = new CDbCriteria; //select $criteria->select = '*';//默认* $criteria->select = 'id,na ...

  6. 常用 Git 命令文档和命令

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3IAAAEVCAIAAAAq20B9AAAgAElEQVR4nOydd3wUxfvH93p6gQRCCF ...

  7. linux时间格式化

    echo `date +'[%Y-%m-%d %H:%M:%S]'`

  8. [Algorithms] Topological Sort

    Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...

  9. Visual Assist X助手的一些使用技巧和快捷键

    部分快捷键 Shift+Alt+F // Find References 查找引用 Shift+Alt+S // FindSynbolDialog打开查找符号对话框 Alt+G // GotoImpl ...

  10. 解决EF6中表名变成复数的情况

    在用EF6 时,在进行数据调用的时候,总提示数据表名对象错误.. 解决此问题需要在继承DbContext的实体类中 加入: using System; using System.ComponentMo ...