博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
跟着Alex老师学习抄了一遍shopping_list的购物程序
阅读量:4561 次
发布时间:2019-06-08

本文共 1832 字,大约阅读时间需要 6 分钟。

这段小程序基本上涵盖了,老师最近讲解的while循环,if,else,逻辑判断。还有重要的一点就是列表。并且在列表里面取下标。

取下标的方法,老师讲了两种。1、print (product_list.index(item),item)   #这样可以显示下标,最基本的方法

              2、利用enumerate来枚举列表中下标,具体表示方法如下。for index, item in enumerate (product_list):

                                                                                                          print (index,item)

利用取下标的方法,可以很容易取到列表里产品的序号。

另外就是字体去颜色,这个是需要死记的。

\033[32;1m %s \033[0m 具体参照上面这个。 附代码。
__author__ = "david.z"product_list = [    ('苹果手机',5800),    ('苹果电脑',9800),    ('自行车',800),    ('高档手表',10600),    ('辛巴克咖啡',31),    ('Python书籍',120),]shopping_list = []salary = input("输入你的工资:")while True:    if salary.isdigit():        salary = int(salary)        while True:            for index, item in enumerate(product_list):                #print(product_list.index(item),item)                print (index,item)            user_choice = input("请选择您要购买的商品?>>>:")            if user_choice.isdigit():                user_choice = int(user_choice)                if user_choice < len(product_list) and user_choice >= 0 :                    p_item = product_list[user_choice]                    if p_item[1] <= salary: #买得起                        shopping_list.append(p_item)                        salary -= p_item[1]                        print("你所购买的商品为 \033[32;1m%s\033[0m,你的余额还剩 \033[31;1m%s\033[0m"%(p_item,salary))                    else:                        print ("\033[41;1m您的余额仅剩%s,请尽快充值!!\033[0m"%salary)                else:                    print("您选择的[%s]不存在!!!"%user_choice)            elif user_choice == 'q'or user_choice == 'Q':                print("---------shopping_list-----------")                for p in shopping_list:                    print(p)                print("您的余额为:",salary)                exit()            else:                print("您的输入有误")    else:        print("您的输入有误,请重新输入!!")    break

 

转载于:https://www.cnblogs.com/davidz/p/8306058.html

你可能感兴趣的文章
软件包管理:rpm命令管理-安装升级与卸载
查看>>
旋转图像
查看>>
每日一小练——数值自乘递归解
查看>>
qq登陆错误提示
查看>>
bzoj 1192: [HNOI2006]鬼谷子的钱袋 思维 + 二进制
查看>>
没写完,没调完,咕咕咕的代码
查看>>
Android Studio使用技巧:导出jar包
查看>>
Problem E. TeaTree - HDU - 6430 (树的启发式合并)
查看>>
Kafka序列化和反序列化与示例
查看>>
win10下VS2010中文输入法切换为英文卡死
查看>>
retinex相关代码汇总
查看>>
Cortex-M3 异常返回值EXC_RETURN
查看>>
kettle 转换字段遇到问题(couldn't get row from result set)——摘
查看>>
nginx首页根据IP跳转
查看>>
【2019-08-20】有点目标,有点计划,有点目的
查看>>
【2019-09-10】美,真的跟年龄无关
查看>>
【2019-09-28】少,但更好
查看>>
【2019-09-13】耐心观察是一种技能
查看>>
mysql数据库2-常用命令
查看>>
安卓开发环境搭建(转)
查看>>