1. 程式人生 > >python全棧筆記-day02-listExercise1

python全棧筆記-day02-listExercise1

#建立一個空列表,命名為names,往裡面新增old_driver,rain,jack,shanshan,peiqi,black_girl元素

names = [ 'old_driver','rain','jack','shanshan','peiqi','black_girl']
names.insert(-1,'alex')
names[3] = '姍姍'
names.insert(2,['oldboy','oldgirl'])
#print(names.index('peiqi'))
new_names = [1,2,3,4,2,5,6,2]
names.extend(new_names)
#print(names + new_names)

# print(names)

# print(names[4:8])
# print(names[2:11:2])
# print(names[-3:])

# print('--------loop----------')
# #count = 0
# for index,i in enumerate(names):
#     print(index, i)
 #   print(i)
 #   count += 1
#     if count % 2 == 0:
#         names[count] = -1
# print(names)

# print(names[names.index(2)+1:].index(2)+names.index(2)+1)

# 購物車模型
products = [['iphone',6888],['MacPro',14800],['小米6',2499],['coffee',31],['book',80],['NIKE shoes',799]]
print('----------商品列表----------')
for index,i in enumerate(products):
    print('%s.  %s       %s'%(index, i[0], i[1]) )

cart = []
run_flag = True#標誌位
while run_flag:
    purchase = input('請輸入要放入購物車的商品編號:')
    if purchase.isdigit():#判斷是否數字
        purchase = int(purchase)
        if purchase >=0 and purchase < len(products):
            cart.append(products[purchase])
        else:
            print('商品不存在')
    elif purchase == 'q':
        if len(cart) > 0:
            print('----------購物車----------')
            for index,k in enumerate(cart):
                print('%s.  %s       %s'%(index, k[0], k[1]) )
            # break
            run_flag = False
        else:
            print('購物車為空')
            run_flag = False
    else:
        print('輸入錯誤')