Python day2 ---python基础2

本节内容

  1. 列表、
  2. 元组操作
  3. 购物车程序
  4. 字符串操作
  5. 字典操作
  6. 3级菜单
  7. 作业(购物车优化)

1. 列表操作

 

1.定义列表
names = ['Alex',"Tenglan",'Eric']

  

2.追加

 

3.插入


 

4.修改


  

5.打印元素

  

6.切片

      

 

 

  

7.索引(获取下标) 和统计

 

8.删除 和 清除

 

       

 

 

  

 

9.翻转和排序

  

10.扩展


  

 

11.Copy

 

12.浅copy ,深copy

 

 

 

 

 

13.循环,打印列表

  

 

14.步长切片

 

 


 

2.元组操作

元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表

它只有2个方法,一个是count,一个是index,完毕。

  

 

3.购物车程序

请闭眼写出以下程序。
程序:购物车程序
需求:
1. 启动程序后,让用户输入工资,然后打印商品列表
2. 允许用户根据商品编号购买商品
3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 

可随时退出,退出时,打印已购买商品和余额

 

 


  

# coding=utf-8
# Author:L

product_list = [
    ("Iphone",5800),
    ("Mac pro",1200),
    ("Bike",100),
    ("watch",1000),
    ("Coffee",12),
    ("Alex Python",120)
]
shopping_list = []
salary = input("Input your salary: ")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(product_list):
            #print(product_list.index(item),item)
            print(index,item)
        user_choic = input("选择要买的?》》》:")
        if user_choic.isdigit():
            user_choic = int(user_choic)
            if user_choic < len(product_list) and user_choic >= 0:
                p_item = product_list[user_choic]
                if p_item[1] <= salary: #买的起
                    shopping_list.append(p_item)
                    salary -= p_item[1]
                    print("Added %s into your shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )
                else:
                    print("\033[32;1m你的余额不足,只有[%s]\033[0m"%(salary))
            else:
                print("product code [%s] is not exist",user_choic)
        elif user_choic == "q":
            print("--------shopping list-------")
            for p in shopping_list:
                print(p)
            print("----------------------------")
            print("Your current balance:",salary)
            exit()

        else:
            print("invalid option")
else:
    print("please enter number,try again...")
go_shopping

 

 

 

  

 知识小点:

1.取商品下标 enumerate (product_list)

 

 


 

2.判断是不是数字

 

  

 

3.列表长度len

 

4.高亮显示

 

 


 

5.退出

 


  

 

4.字符串操作

 特性:不可修改 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.字典操作

字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。

字典的特性:

  • dict是无序的
  • key必须是唯一的,so 天生去重
1.语法:
info = {
    'stu1101': "TengLan Wu",
    'stu1102': "LongZe Luola",
    'stu1103': "XiaoZe Maliya",
}
 

 

  

 

2.查找


  

 

3.改,增


 

4.删除


  

5.多级字典嵌套及操作

 

修改

 

 




 

6.Key value item

 


  

 

7.update

 


  

  

8.初始化字典

#通过一个列表生成默认dict,

 

 

  

 

9.循环

 


  

  

6.  3级菜单

 

  

# coding=utf-8
# Author:L

data = {
    "北京":{
        "朝阳区":{},
        "天安区":{},
        "玄武区":{},
    },
    "西安":{
        "雁塔区":{
            "电子正街":["鸿星尔克","特步"],
            "高新区":["百度","腾讯"],
            "科技路":["西部","欧朋"],
        },
        "碑林区":{
            "太白路":["西安理工","西安交大"],
            "金华路":["火腿","飞信"],
            "南二环":["后卫寨","鱼化寨"]
        },
        "长安区":{
            "111":["aa","agag"],
            "222":["afdas","ag"],
            "333":["afd","ag"]
        },
    },
    "上海":{
        "虹桥区":{},
        "陆家嘴区":{},
        "海港区":{},
    },
}

while True:
    for i in data:
        print(i)
    choice = input("选择进入1>>:")

    if choice in data:
        while True:
            for i2 in data[choice]:
                print("\t",i2)
            choice2 = input("选择进入2>>:")

            if choice2 in data[choice]:
                while True:
                    for i3 in data[choice][choice2]:
                        print("\t\t",i3)
                    choice3 = input("选择进入3>>:")

                    if choice3 in data[choice][choice2]:
                        for i4 in data[choice][choice2][choice3]:
                            print("\t\t\t",i4)

                        choice4 = input("this is laster:按b返回>>:")
                        if choice4 == "b":
                            pass #什么也不做,占位符,防止出错
                    if choice3 == "b":
                        break
            if choice2 == "b":
                break
完整程序1.0

 

# coding=utf-8
# Author:L

data = {
    "北京":{
        "朝阳区":{},
        "天安区":{},
        "玄武区":{},
    },
    "西安":{
        "雁塔区":{
            "电子正街":["鸿星尔克","特步"],
            "高新区":["百度","腾讯"],
            "科技路":["西部","欧朋"],
        },
        "碑林区":{
            "太白路":["西安理工","西安交大"],
            "金华路":["火腿","飞信"],
            "南二环":["后卫寨","鱼化寨"]
        },
        "长安区":{
            "111":["aa","agag"],
            "222":["afdas","ag"],
            "333":["afd","ag"]
        },
    },
    "上海":{
        "虹桥区":{},
        "陆家嘴区":{},
        "海港区":{},
    },
}

exit_flag = False
while not exit_flag:
    for i in data:
        print(i)
    choice = input("选择进入1>>:")
    if choice in data:
        while not exit_flag:
            for i2 in data[choice]:
                print("\t",i2)
            choice2 = input("选择进入2>>:")
            if choice2 in data[choice]:
                while not exit_flag:
                    for i3 in data[choice][choice2]:
                        print("\t\t",i3)
                    choice3 = input("选择进入3>>:")
                    if choice3 in data[choice][choice2]:
                        for i4 in data[choice][choice2][choice3]:
                            print("\t\t\t",i4)
                        choice4 = input("this is laster:按b返回>>:")
                        if choice4 == "b":
                            pass #什么也不做,占位符,防止出错
                        elif choice4 == "q":
                            exit_flag = True
                    if choice3 == "b":
                        break
                    elif choice3 == "q":
                        exit_flag = True
            if choice2 == "b":
                break
            elif choice2 == "q":
                exit_flag = True
    elif choice == "q":
        exit_flag = True
完整程序2.0

 

 
 
            
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

 

 7. 作业(购物车优化)

 

posted @ 2017-07-15 21:58  venicid  阅读(250)  评论(0编辑  收藏  举报