image.png
names = ("one","two","three","two")
print(names.count("two"))#获取此元素的个数
print(names.index("two"))#获取此元素最高位的索引,也就是最右边的索引。
image.png
#购物车程序练习
product_list = [
("Iphone",5800),
("Mac Pro",9800),
("Bike",800),
("Watch",1800),
("Alex python",120)
]
shopping_list = []
salary = input("InPut your salary:")
if salary.isdigit():
salary = int(salary)
while True:
#写法一:item是元组中的每一个元素
#for item in product_list:
#print(product_list.index(item),item)
#写法二:index 是一个元组(里面包含索引和元组中的元素)
#for index in enumerate(product_list):
#print(index)
#写法三:index是索引,item是元素
for index,item in enumerate(product_list):
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("Add %s into shopping_list,your current 余额:\033[31;1m%d\033[0m"%(p_item,salary))#\033[31;1mXX\033[0m高亮红色,31代表红色
else:
print("\033[41;1m你的余额:{0}\033[0m".format(salary))
else:
print("此商品不存在")
elif user_choice == "q":
print("-----------------shopping_list-------------")
for p in shopping_list:
print(p)#购买的商品
print("你当前账户余额:%d"%(salary))
exit()
else:
print("输入的无效字符")