day2学习内容汇总
参考:https://www.cnblogs.com/alex3714/articles/5717620.html
1. 列表常用方法:
|
切片:names[1:3],-1是最后一个,可以有三个参数,类似range,0和-1可以省略; 追加:append:直接使用names.append(“abd") 插入:insert 删除;del;remove;pop; 扩展;extend 拷贝;copy--浅copy只拷贝一层;--实例就是--共同储蓄账户; person = ['name',['a',100]] 排序:sort() 反转:reverse; 获取下标:index方法; |
2. 元组,又被称为只读列表,创建之后就不能修改;
3.购物车程序:几个注意点,输入判断,采用字符判断函数isdigit,另一个是长度判断,这个比较容易遗忘;索引函数enumerate;常用高亮显示:格式为,\033[显示方式;前景色;背景色m,\033[0m
--显示方式:0(默认值)、1(高亮)、22(非粗体)、4(下划线)、24(非下划线)、5(闪烁)、25(非闪烁)、7(反显)、27(非反显)
--前景色:30(黑色)、31(红色)、32(绿色)、33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)
--背景色:40(黑色)、41(红色)、42(绿色)、43(黄色)、44(蓝色)、45(洋红)、46(青色)、47(白色)
product_list = (
['Iphone',3900],
['Iwatch',2500],
['AirPods',900],
['Mouse',300],
['Coffee',50]
)
shopping_car = []
salary = input("Please input you salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index,value in enumerate(product_list):
print(index,value)
choice = input("Please input your choice:")
if choice.isdigit():
choice = int(choice)
if choice < len(product_list) and choice >= 0:
product_list_choice = product_list[choice]
print(product_list_choice)
if salary > product_list_choice[1]:
salary-= product_list_choice[1]
shopping_car.append(product_list_choice)
else:
print("Your balance is not enough, the balance is %d" %(salary))
else:
print("not exist")
elif choice == 'q':
print("product had bought as below,and your balance is \033[1;31m%d\033[0m " % (salary))
for x in shopping_car:
print(x)
exit(1)
else:
print("Please input a digit or 'q'")
else:
print("Please input a digit")
4.字符串常用操作,引用Alex博客
name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 '---------------------Alex Li----------------------' name.count('lex') 统计 lex出现次数 name.encode() 将字符串编码成bytes格式 name.endswith("Li") 判断字符串是否以 Li结尾 "Alex\tLi".expandtabs(10) 输出'Alex Li', 将\t转换成多长的空格 name.find('A') 查找A,找到返回其索引, 找不到返回-1 format : >>> msg = "my name is {}, and age is {}" >>> msg.format("alex",22) 'my name is alex, and age is 22' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("alex",22) 'my name is 22, and age is alex' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=22,name="ale") 'my name is ale, and age is 22' format_map >>> msg.format_map({'name':'alex','age':22}) 'my name is alex, and age is 22' msg.index('a') 返回a所在字符串的索引 '9aA'.isalnum() True '9'.isdigit() 是否整数 name.isnumeric name.isprintable name.isspace name.istitle name.isupper "|".join(['alex','jack','rain']) 'alex|jack|rain' maketrans >>> intab = "aeiou" #This is the string having actual characters. >>> outtab = "12345" #This is the string having corresponding mapping character >>> trantab = str.maketrans(intab, outtab) >>> >>> str = "this is string example....wow!!!" >>> str.translate(trantab) 'th3s 3s str3ng 2x1mpl2....w4w!!!' msg.partition('is') 输出 ('my name ', 'is', ' {name}, and age is {age}') >>> "alex li, chinese name is lijie".replace("li","LI",1) 'alex LI, chinese name is lijie' msg.swapcase 大小写互换 >>> msg.zfill(40) '00000my name is {name}, and age is {age}' >>> n4.ljust(40,"-") 'Hello 2orld-----------------------------' >>> n4.rjust(40,"-") '-----------------------------Hello 2orld' >>> b="ddefdsdff_哈哈" >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则 True
5.字典;特性,无序的;
|
1.--增/改:info['a']='1' --删:info.pop del info[] --查,使用 in或者get方法,防止报错; 2.其他方法: -setdefault,就是看存不存在,存在返回,不存在就返回默认值,同时新建一个; —另一种方法: |
6. 程序: 三级菜单
要求:打印省、市、县三级菜单,可返回上一级,可随时退出程序
menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, 'youku':{}, }, '上地':{ '百度':{}, }, }, '昌平':{ '沙河':{ '老男孩':{}, '北航':{}, }, '天通苑':{}, '回龙观':{}, }, '朝阳':{}, '东城':{}, }, '上海':{ '闵行':{ "人民广场":{ '炸鸡店':{} } }, '闸北':{ '火车战':{ '携程':{} } }, '浦东':{}, }, '山东':{}, } while True: for i in menu: print(i) choice = input("请选择省:") if choice in menu: while True: for j in menu[choice]: print('\t'+j) choice1 = input("\t请选择:") if choice1 in menu[choice]: while True: for k in menu[choice][choice1]: print('\t\t'+k) choice2 = input("\t\t请选择:") if choice2 in menu[choice][choice1]: while True: for l in menu[choice][choice1][choice2]: print('\t\t',l) choice3 = input("\t\t返回上一级,请按b:") if choice3 == 'b': break elif choice3 == 'q': exit() elif choice2 == 'b': break elif choice2 == 'q': exit() elif choice1 == 'b': break elif choice1 == 'q': exit() elif choice == 'q': exit()
7. 作业:
—用户入口:商品信息存在文件里;已购商品,余额记录保存;
— 商家入口:可以添加商品,修改商品价格

浙公网安备 33010602011771号