元组 字体高亮 购物车练习

元组就是列表,但是不能增删改,只能切片,只能查

所以元组又叫做只读列表

元组只有2个方法,一个是count 一个是 index
count 统计
index 获取下标

使用元组的情况 , 存的一些值,不希望被改变 ,也是提醒别人,元组不希望被修改

1、启动程序,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户购买商品后,检测余额是否足够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额

enumerate 返回列表元素下表和元素

判断列表长度 length len(shopping_list)

-e 用来开启echo中的转义
e 或 33 来输出Esc符号
设置颜色的格式: e[背景色;前景色;高亮m
33[背景色;前景色;高亮m
恢复默认为 e[0m
其中背景色可以被以下数字替换
第一个参数:
0 透明(使用终端颜色),1 高亮 40 黑, 41 红, 42 绿, 43 黄, 44 蓝 45 紫, 46 青
绿, 47白(灰)
第二个参数:
前景色(也就是文本的颜色)可以被以下数字替换
30 黑 31 红, 32 绿, 33 黄, 34 蓝, 35 紫, 36 青绿, 37 白(灰)
第三个参数:
高亮是1,不高亮是0
第四个参数为m:
注意m后面紧跟字符串。
----------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Author:summer_han
product_list=[
("Iphone X",8999),
("Mac pro",17999),
("Bike",999),
("Balance car",1999),
("Book",59),
("IWatch",3999)
]
shopping_list = []
salary=input("Please input your balance:" )

if salary.isdigit():
salary = int(salary)

while True:
for menu in product_list:
print(product_list.index(menu),menu)
user_choice = input("please input your shopping number:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
product_item = product_list[user_choice]
print(product_item)
if salary >= product_item[1]: #买得起
salary = salary - product_item[1]
shopping_list.append(product_item)
print("Adding %s into your shopping cart,your current balance is %s" % (product_item[0],salary))
else:
print("33[42;1m余额不足,您的余额为[%s]33[0m" % salary )
else:
print("您输入的数字超出范围,请选择商品范围为0-[%s]" % len(product_list))
elif user_choice == 'q':
print("33[42;1m----------shopping list---------------33[0m")
for p in shopping_list:
print(p)
print("Your balance is 33[42;1m %s 33[0m" % salary)
exit()

else:
print("Invalid option.")

原文地址:https://www.cnblogs.com/summer-han/p/7954508.html