Python 学习 day4

购物车程序的详解

shop = [
	['iphone8',5000],
	['kndle',988],
	['ps4 pro',2800],
	['psv',1200],
	['mp3',100]
]
shop_car = []
saving = input("请输入预算:")
# 验证用户输入是否正确
if saving.isdigit():
	saving=int(saving)
while True:
# 列出商品列表
	for i,v in enumerate(shop,1):
		print(i,"-",v)
		# 让用户选择商品到购物车
	choice = input("请选择商品编码到购物车:[确认:q]")
	if choice.isdigit():
		choice=int(choice)
		if choice > 0 and choice<=len(shop):
			p_item = shop[choice-1]
			if p_item[1]<saving:
				saving -= p_item[1]
				shop_car.append(p_item)
				print("您选购的商品%s,剩余%s" % (p_item,saving))
			else:
				print("您的余额不足,剩余%s" % saving)
	# 让客户确认购物车内购买的商品
	elif choice=="q":
		print("----您的购物车清单----")
		for i in shop_car:
			print(i)
		# 让客户确认是否购买并打印购物车和余额
		guess = input("是否确认购买:q/n")
		if guess == "q":
			print("您已经购买%s,余额%s" % (shop_car,saving))
			break
		# 客户没有确认退出
		else:
			print("欢迎再来")
		break
	else:
		print("输入编码无效")
else:
	print('输入金钱无效')

字典

  • 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以无序存储的且key必须是可哈希的。可哈希表示不可变类型如:数字、字符串、元祖
    • 不可变类型:整型,字符串,元祖
    • 可变类型:列表,字典
dict1 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
print(dict1["name"]
# 这里会报错,key值不能是可哈希的值
dict1 = {[1,2]:"alex","age":29,"hobby":"game","is_handsome":True}
# 不报错,value值可以随意
dict1 = {"name":["alex",29],"age":29,"hobby":"game","is_handsome":True}
dict2 = dict([["name","alex"]])

字典的操作

dict1 = {'name':"alex"}
dict1["age"]=18
print(dict1)
# 如果键值存在不改变,返回值是原值
rst =dict1.setdefault("age","book")
print(rst) # 18 
# 键不存在,增加新的键值,返回值是相应的值
rst1 = dict1.setdefault("hobby","book")
print(rst1) # book
dict1 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
print(dict1["name"])
print(list(dict1.keys()))
print(list(dict1.values()))
print(list(dict1.items()))
dict1 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
dict1["name"]=29
dict2 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
dict3 = {"1":"111","2":"222","3":"333"}
dict2.update(dict3)
dict2 = {"name":"alex","age":29,"hobby":"game","is_handsome":True}
# 清空字典
dict2.clear()
print(dic2)
# del 删除字典中指定键值对
del dict2["name"]
# pop 删除字典中指定键值对返回该键值对的值
print(dict2.pop("age"))
# popitem 随机删除某组键值对,并以元祖方式返回
a = dict2.popitem()
print(a,dict2)
  • 其他操作方法
dic6 = dict.fromkeys(["host1","host2","host3","host4"],"test")
  • 字典循环遍历
dic5 = {"name":"alex","age":18,"hobby":"python"}
for i in dic5:  # 推荐这种
	print(i,dic5[i])
for i,v for dic5.items(): # 这样占内存大不推荐使用
	print(i,v)
  • 字符串操作
#1. 重复输出字符串
a = ("hello" * 2)
print(a)
# 2. 通过索引
a = ("helloworld"[2:])
print(a)
# 3. 关键 in 判断是否在字符串内,如果在返回True
print(123 in "123456hello"# 4. 格式化字符串
print("alex is a good teacher")
print("%s is a good teacher" % "alex")
# 5. 字符串的拼接
a = "123"
b = "abc"
c = "444"
# .join方法拼接
d = "-----".join([a,b,c])
print(d)
  • 字符串的内置方法:
st = "hello kitty{name} is {age}"
print(st.count("l")) # 统计元素个数
print(st.capitalize()) # 首字母大写
print(st.center(50,"-")) # 居中元素
print(st.casefold()# 全部大写 用不到
print(st.encode()) # 解码
print(st.endswitch("tty3")) # 判断是否以某个内容结尾
print(st.startswitch("he")) # 判断是否以某个内容开头
print(st.expandtabs(tabsize=10)) # 转换\t 几个空格 用不到
print(st.find("t")) # 查找到第一个元素,并将索引值返回
print(st.format(name = "Alex",age = 37)) # 格式化输出的另一个版本
print(st.format_map({"name":"alex","age":22}) # 格式化输出的字典版本
print(st.index("t")) # 与find的区别就是报错
print("abc456".isalnum()) # 判断是否是字母和数字 用不到
print("1231234".isdeciaml()) # 判断是否为十进制 用不到
print("126323178".isdigit()) # 判断是否为整型数字
print("abc".isidentifier()) # 判断是否非法变量
print("abc".islower()) # 判断是否为小写
print("ABC".isupper()) # 判断是否为大写.
print("  ".isspace()) # 判断是否为空格
print("My Tittle".istittle()) # 判断是否为标题
print("My Tittle".lower()) # 所有字符变小写
print("My Tittle".upper())  # 所有字符变大写
print("My Tittle".swapcase())  # 大小写反转
print("My Tittle".ljust(50,"*")) # 向左添加字符串
print("My Tittle".rjust(50,"*")) # 向右添加字符串
print("\tMy Tittle\n".lstrip()) # 去掉左边的换行符
print("\tMy Tittle\n".rstrip()) # 去掉右边的换行符
print("\tMy Tittle\n".strip()) # 去掉所有的换行符
print("My Tittle\n".replace("Tittle","lesson")) # 替换字符
print("My Tittle".split(" ")) # 以左分割成列表
print("My Tittle".rsplit(" "))  # 向右分割成列表
print("My Tittle tittle".title()) # 把所有字母首开头变字母
  • 重要的字符串方法
print(st.count("l")) # 统计元素个数
print(st.startswitch("he")) # 判断是否以某个内容开头
print(st.center(50,"-")) # 居中元素
print(st.find("t")) # 查找到第一个元素,并将索引值返回
print(st.format(name = "Alex",age = 37)) # 格式化输出的另一个版本
print("My Tittle".lower()) # 所有字符变小写
print("My Tittle".upper())  # 所有字符变大写
print("\tMy Tittle\n".strip()) # 去掉所有的换行符 ******
print("My Tittle\n".replace("Tittle","lesson")) # 替换字符
print("My Tittle".split(" ")) # 以左分割成列表

作业:

  • 程序:三级菜单
  • 要求
  1. 打印省、市、县三级菜单
  2. 可返回上一级
  3. 可随时退出程序

猜你喜欢

转载自blog.csdn.net/weixin_43730662/article/details/84649013