python自动化测试-py基础02

Day2笔记
 
打开builtin的方法有两种:
 
1、选中要查看的方法,ctrl + 左键打开builtin的方法,是c语言实现的
2、选中要查看的方法,点击鼠标右键
 
 
一、列表、元组操作
列表是最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
 
通过下标访问;列表中的元素下标从0开始计数
 
切片:取多个元素
 
追加
插入
 
修改
 
删除:
注意:remove()方法只删除第一次在列表中出现的指定元素
 
扩展
 
拷贝
 
统计
 
排序&翻转
 
获取下标
 
 
列表的for循环
 
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
它只有2个方法,一个是count,一个是index
 

程序练习 

请闭眼写出以下程序。
程序:购物车程序
需求:
  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
  4. 可随时退出,退出时,打印已购买商品和余额
 
# _Author_= MingshuangRen
'''
程序练习
请闭眼写出以下程序
程序:购物车程序
需求:
1、启动程序,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
'''
 
product_list = [
('iphone', 5800),
('Mac Pro', 9800),
('Bike', 800),
('Watch',10600),
('Coffee',31),
('Alex python', 80)
]
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_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 cart,your current balance is %s" %(user_choice,salary))
else:
print("你的余额只剩[%s]啦,还买个毛线" %(salary))
else:
print("prouct code [%s] is not exist"% user_choice)
elif user_choice =='q':
print('-----------shopping list-------------')
for p in shopping_list:
print(p)
print("Your current balance:",salary)
exit()
else:
print("invalid option")
 
 
 
二、字符串的操作
特性:不可修改
# _Author_= MingshuangRen
 
name = "my name is rms ren"
 
print(name.capitalize()) #首字母大写 My name is rms ren
print(name.count("m")) # 统计m出现次数 3
print(name.center(50,"-")) # 输出'------------------my name is rms------------------'
print(name.endswith("en")) # 判断字符串是否以en结尾
print(name.expandtabs(tabsize=30))
print("rms\tren".expandtabs(10)) #输出'rms ren', 将\t转换成多长的空格
print(name.find("name")) #查找name,找到返回其索引, 找不到返回-1
 
#format:
msg = "my name is {}, and age is {}"
print(msg.format('rms',23)) #my name is rms, and age is 23
 
msg = "my name is {1}, and age is {0}"
print(msg.format('rms',23)) #my name is 23, and age is rms
 
msg = "my name is {name}, and age is {age}"
print(msg.format(name='rms',age=23)) # my name is rms, and age is 23
 
#format_map:
msg = "my name is {name}, and age is {age}"
print(msg.format_map({'name':'rms',"age":30})) #my name is rms, and age is 30
 
print(name.index('m')) # 返回m所在字符串的索引
print('abc123'.isalnum()) # 返回True,判断是否包含字母数字
print('abc'.isalnum()) # 返回True,判断是否包含字母数字
print('123'.isalnum()) # 返回True,判断是否包含字母数字
print('abS'.isalpha()) # 返回True,判断是否只包含字母
print('1A'.isdecimal()) # 返回False,判断是否全部为十进制数
print('9'.isdigit()) #返回True,判断是否是整数
print('a 1A'.isidentifier()) # 返回False,判断是不是一个合法的标志符,即是否符合变量命名规则
print('33A'.isnumeric()) #返回False,判断是不是一个合法的标志符,即是否符合变量命名规则
print(' '.isspace())#返回True,判断输入的字符是否全为空格
print('My Name Is '.istitle()) #返回True,判断输入字符每个单词的首字母是否为大写
print('My Name Is '.isprintable()) #返回True,tty file,drive file
print('My Name Is '.isupper()) #返回False,判断是不是都为大写
print('My Name Is '.join("==")) # =My Name Is =
print("|".join(['alex','jack','rain'])) # alex|jack|rain
print(name.ljust(50,'*')) #左边不足50的用*补充 my name is rms ren********************************
print(name.rjust(50,'-')) #右边不足50的用*补充 --------------------------------my name is rms ren
print('rms'.lower()) #全部变为小写
print('rms'.upper()) #全部变为大写
print('\nrms'.lstrip()) #去除左边的\n
print('rms\n'.rstrip()) #去除左边的\n
print(' rms\n'.strip()) #去除两边的\n
 
p = str.maketrans("arcmes","123456") #将后面的在字符和前面的字符一一对应
print("rms ac".translate(p)) # 246 13
 
print("rms ming".replace('m','M',2)) #将字符串中出现的前两个m用M替代 rMs Ming
print("rms ming".rfind('m')) #4 打印最右边的m的下标
print('1+2+3+4'.split('+')) # ['1', '2', '3', '4']
print('1+2\n+3+4'.splitlines()) #['1+2', '+3+4'] 根据空格分割
print("rms Ming".swapcase()) # RMS mING 大小写互换
print("rms Ming".title()) #Rms Ming
print("rms ming".zfill(50)) #000000000000000000000000000000000000000000rms ming 向左填充
print('-------')
 
三、字典操作
字典一种key - value 的数据类型
语法:
字典的特性:
  • dict是无序的
  • key必须是唯一的,so天生去重
 
增加
 
修改
 
删除
 
 
查找
 
如果一个key不存在,就报错,get不会,不存在只返回None
多级字典嵌套及操作
 
其它姿势
 
循环dict
 
程序练习
程序: 三级菜单
要求: 
  1. 打印省、市、县三级菜单
  2. 可返回上一级
  3. 可随时退出程序
# _Author_= MingshuangRen
'''
程序:三级菜单
要求:
    1、打印省、市、县三级菜单
    2、可返回上一级
    3、可随时退出程序
'''
data = {
    "北京":{
        "昌平":{
            "沙河":["oldboy","test"],
            "天通苑":["链家地产","我爱我家"],
        },
        "朝阳":{
            "望京":["奔驰","陌陌"],
            "国贸":["CICC","HP"],
            "东直门":["Advent","飞信"],
        },
        "海淀":{},
    },
    "山东":{
        "德州":{},
        "青岛":{},
        "济南":{},
    },
    "广东":{
        "东莞":{},
        "常熟":{},
        "佛山":{},
    },
}
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",i4)
                        choice4 = input("最后一层,按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
View Code

 

posted @ 2018-11-23 14:48  taoziya  阅读(359)  评论(0)    收藏  举报