python 修行之路(一)(购物车练习)

#Author:Mr Li

shopping_name = [
                    ['Iphone',5800],
                    ['surface',8000],
                    ['huaweu',4000],
                    ['meizu',2000]
]
shopping_list=[]

salary = int(input('please input your salary '))
while 1 :
    print('please select your shopping\n')
    for i in range(4):
        print(shopping_name[i])
        print('\n')
    user = input("请选择商品:")
    if user.isdigit():
        user = int(user)
        if user < len(shopping_name) and user >=0:
            s_item = shopping_name[user]
            if salary >= s_item[1]:
                salary -= s_item[1]
                shopping_list.append(s_item)
                print('you have shopping_name[user]\n')
            else:
                print('shopping faild\n')
                print("your banlance is %d yuan,还买个毛线啊\n"% salary)
        else:
            print('valid value\n')
    if user == 'q':
        print("your shopping list is:")
        for a in shopping_list:
            print(a)
        exit()

 
  
以上是一个简单的购物程序,主要用到了列表的嵌套,变量的存储以及将字符型转换为int型等相关知识。上面shopping_name 利用了列表嵌套,列表嵌套中的内列表可以使用【】,
也可以使用()。 


你可能感兴趣的:(python 修行之路(一)(购物车练习))