第二章列表练习题

时间:2023-02-24 00:08:34

第二章列表练习题

 

 

# # 练习2
# names.insert(-1, 'alex')
# print(names)
# # 练习3
# names[3]='姗姗'
# print(names)
#
# # 练习4
# names.insert(2, ['oldboy', 'oldgirl'])
# print(names)
#
# # 练习5
# print(names.index('peiqi'))
#
# 练习6
# num = [1, 2, 3, 4, 2, 5, 6, 2]
# names = names + num
#
# print(names)
#
# # 练习7
# print(names[4:7])
#
# # 练习8
# print(names[2:10:2])
#
# # 练习9
# print(names[13:])
# # 练习10
# for i in names:
#     print(i, names.index(i))
# 练习11

# for i in names:
#     # print(f'索引值为:{names.index(i)},对应的值为{i}')
#     if names.index(i) % 2 ==0:
#         names.index(i) = -1
#         print(names)
# 练习13

products = [['Iphone8', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee', 31], ['Book', 80], ['Nike Shoes', 799]]
# for i in products:
#     for a in i:
#         print(products.index(i), a, end='')
#     print('\n')

# print('----------商品列表------------')
# for index, i in enumerate(products):
#     print(index, i[0], i[1])
flag = False  #标志位
res = []
while  not flag:# 取反
    print('----------商品列表------------')
    for index, i in enumerate(products):
        print(index, i[0], i[1])
    num = input('输入商品编号购买,或者按q退出:')
    if num.isdigit():
        num = int(num)
        if num <= len(products):
            res.append(products[num])
            # products.pop(num)
        else:
            print('商品不存在')
    elif num == 'q':
        if len(res) > 0:
            print('----------商品列表------------')
            for index, i in enumerate(res):
                print(index, i[0], i[1])

        flag = True