知识点:
# name.remove(element) #删除
# del name[2] #删除
# del name #删除列表
# name.clear() #清空列表
# name.extend() #扩展
name1=["a1","a2"]
name2=["b1","b2"]
print(name1+name2) #扩展
name1.extend(name2) #扩展
print(name1)
name2.reverse() #反转
print(name2)
name2.sort()
print(name2)
name3=["a","v","2","3"]
name3.sort() #排序
print(name3) #字符排序用ascii表的顺序排,python3是字符与数字无法排
name1.clear() #清空内容
print(name1)
布尔分布
条件成立 true
条件不成立,false
字符串常用功能:
name = "\n\t jensen xie \t"
print(name)
print(name.strip()) #脱掉,把前后的空格,\t\n脱掉
print(name.split()) #分割,把一个字符默认按空格分割为列表类型
name = "\n\t jensen xie;alex li \t"
print(name.split(";")) #分割,把一个字符默认按空格分割为列表类型,可以指定分隔符
print(len(name)) # 长度
name1="jensen,zhicong,lizhi"
print(name1.index("h")) # 索引
# 只提取字符串中一部分信息,叫切片
print(name1[0:6])
print(name1[7:14])
print(name1[-5:])
print(name1[0::2]) #2是切的步长
print(name1[0::1])
#2是切的步长
替换
name = ["a","b","c","d","e"]
print(name)
print(name.index("d"))
name[name.index("d")]="dd"
print(name)
name = [] #列表
print(name)
print(type(name))
name = ["苏豪之","秦臻","李志","李志","燕子"]
print(name)
print(name[-1])
print(name.index("李志")) #查询李志的所在位置,元素的下标
# 想取秦臻后面的所有
print(name[1:])
# 统计多少个李志
print(name.count("李志"))
# 追加
name.append("光头")
print(name)
# 插入,在第2个后面
name.insert(2,"玉米")
print(name)
# 删除第4个
print(name.pop(2))
print(name)
--------------------------------------------------------------
# 作业购物车
# 1、启动程序后,让用户输入工资,然后打印商品列表
# 2、允许用户根据商品编号购买商品
# 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
# 4、可随时退出,退出时,打印已购买商品和余额
number=[1,2,3,4,5]
goods=["鱼","手机","米","平板","电视"]
price=[30,6000,15,3000,4000]
wages=input("请输入你的预算:")
for i in range(0,5):
print(number[i],"、商品:",goods[i],",价格:",price[i],"元")
buy1=[]
buy=[0,0,0,0,0]
goods1=[0,0,0,0,0]
buy1=input("要买商品的编号,用‘,‘隔开:")
a1=len(buy1)
print(a1)
for j in range(9):
t1 = 2 * j + 1
if t1
<= a1:
buy[j]=int(buy1[2*j])
else:
break
print(buy)
account=0
for j in range(5):
if buy[j]>0:
account=account+price[buy[j]-1]
goods1[j]=goods[buy[j]-1]
else:
break
print(account)
print(wages)
print(type(account),type(wages)) #两个数字的类型不一样,无法比较
if int(wages)
< account :
print("余额不足,你的预算是:",wages,"你将支付:",account)
else:
order="感谢购买!以下是你的订单:"
for j in range(5):
if str(goods1[j]) !="0":
order=order+str(goods1[j])+","
else:
break
print(order,"余额为:",int(wages)-account)
# 编写登陆接口
# ? 输入用户名密码
# ? 认证成功后显示欢迎信息
# ? 输错三次后锁定
name = "jensen"
passwd = "123"
for i in range(4):
name1 = input("your name:")
passwd1 = input("your passwd:")
if name1
== name and passwd1 == passwd :
print("认证成功后显示欢迎信息")
break
else :
if i+1<3:
print("请重新输入。")
i+=1
continue
else:
print("输错三次后锁定")
break