码代码,6个小时。
# 1. 请用代码实现: 利用下划线将列表的每一个元素拼接成字符串, li = ['alex', 'eric', 'rain']
# li = ['alex', 'eric', 'rain']
# print('_'.join(li))
# 2. 查找列表中元素,移除每个元素的空格,并查找以a或A开头并且以c结尾的所有元素
#
# li = [" a l e c ", " a r i c ", "A l e x", "T o n y ", " r a i n "]
# li_join = '_'.join(li)
# new_li = li_join.replace(' ', '')
# check_li = new_li.split('_')
#
# for i in check_li:
# if i.startswith('a') or i.startswith('A') and i.endswith('c'):
# print(i)
# 3. 写代码,有如下列表,按照要求实现每一个功能
# li = ['alex', 'eric', 'rain']
# print(len(li))
#
# li.append('seven')
# print(li)
#
# li.insert(0, 'Tony')
# print(li)
#
# li[1] = 'Kelly'
# print(li)
# li.remove('eric')
# print(li)
#
# li.pop(1)
# print(li)
#
# del li[2]
# print(li)
# del li[1:4]
# print(li)
# li.reverse()
# print(li)
# for i in range(len(li)):
# print(i)
# for index, i in enumerate(li, start=100):
# print(index, i)
#
# for i in li:
# print(i)
# 4. 写代码,有如下列表,按照要求实现每一个功能
# li = ['hello', 'seven', ['mon', ['h', 'kelly'], 'all'], 123, 446]
# print(li[2][1][1])
#
# li[2][2] = 'ALL'
# print(li)
# 4. 写代码,有如下元组,按照要求实现每一个功能
# tu = ('alex', 'eric', 'rain')
# print(len(tu))
# print(tu[1])
# print(tu[0:2])
#
# for i in tu:
# print(i)
# for i in range(len(tu)):
# print(i)
#
# for index, i in enumerate(tu, start=10):
# print(index, i)
# 6. 有如下变量,请实现要求的功能
# tu = ('alex', [11, 22, {'k1': 'v1', 'k2': ['age', 'name'], 'k3': (11, 22, 33)}, 44])
# 元组可存放多个值,元组中的值不可变,但是如果元组里面还有列表、元组、字典等就可变,属于浅不可变
# tu[0] = 'ALEX'
# 不可变
# 'k2'对应的值是一个列表['age', 'name'],可变
# tu[1][2]['k2'].append('Seven')
# print(tu)
# 'k3'对应的值是一个子元组,可变
# tu[1][2]['k3'] = (11, 22, 33, 'Seven')
# print(tu)
# 7. 字典
# dic = {'k1': 'v1', 'k2': 'v2', 'k3': [11, 22, 33]}
# for i in dic.keys():
# print(i)
#
# for i in dic.values():
# print(i)
# for key,value in enumerate(dic):
# print(key, value)
#
# dic.setdefault('k4', 'v4')
# print(dic)
#
# dic['k1'] = 'alex'
# print(dic)
#
# dic['k3'].append(44)
# print(dic)
#
# dic['k3'].insert(0, 18)
# print(dic)
# 8. 转换
# s = 'alex'
# li = [s]
# print(li)
# tu = (s)
# print(tu)
#
# li = ['alex', 'seven']
# tu = (li[0], li[1])
# tu2 = tuple(li)
# print(tu)
# print(tu2)
#
# tu = ('alex', 'seven')
# li = [tu[0], tu[1]]
# li2 = list(tu)
# print(li)
# print(li2)
#
# li = ['alex', 'seven']
# new_dict = {}
# for index, i in enumerate(li, start=10):
# new_dict[index] = i
#
# print(new_dict)
# 9. 元素分类
# li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
# dic = {'k1': [], 'k2': []}
# for i in li:
# if i > 66:
# dic['k1'].append(i)
# elif i < 66:
# dic['k2'].append(i)
#
# print(dic)
# 10. 输出商品列表,用户输入序号,显示用户选中的商品
# 商品 li = ["手机","电脑","鼠标垫","游艇"]
# 允许用户添加商品
# 用户输入序号显示内容
li = ["手机", "电脑", "鼠标垫", "游艇"]
s = '商品列表'
buy = '购物车商品列表'
shopping_cart = []
exit_flag = False
while not exit_flag:
print(s.center(50, '-'))
for index, i in enumerate(li, start=0):
print('%s. %s' % (index, i))
choice = input('请输入您要选择的商品: (按q键买单或者退出)')
if choice.isdigit():
choice = int(choice)
if choice < len(li):
print('您选中的商品为:', li[choice])
buy_choice = input(",请问是否要添加该商品至您的购物车?(输入'y'添加,否则重新回到商品列表)")
if buy_choice == 'y':
shopping_cart.append(li[choice])
print('商品', li[choice], '已经添加到您的购物车,请继续购物,如需买单请按q键')
else:
pass
else:
print('对不起,您输入的商品编号不存在,请重新输入')
elif choice == 'q':
if len(shopping_cart) == 0:
print('感谢您的光临!')
exit_flag = True
else:
print(buy.center(50, '-'))
for index, i in enumerate(shopping_cart, start=1):
print('%s. %s' % (index, i))
exit_flag = True
else:
print('对不起,请您输入数字编号,谢谢')
c = {
'p1': {'c1': ['d1', 'd2', 'd3'], 'c2': ['d4', 'd5', 'd6']},
'p2': {'c3': ['d7', 'd8', 'd9'], 'c4': ['d10', 'd11', 'd12']}
}
exit_flag = False
p_exit_flag = False
c_exit_flag = False
d_exit_flag = False
t_exit_flag = False
l_exit_flag = False
while not exit_flag:
choice = input('请问您是要查看还是修改地域字典,查看请输入r,修改请输入w,退出请输入q:')
if choice == 'r':
if len(c) > 0:
while not p_exit_flag:
s_province = '省列表' # 此处打印省列表
print(s_province.center(50, '-'))
for i in c.keys():
print(i)
print(s_province.center(50, '-'))
choice_p = input('请问您要查看哪个省的列表?(#号键返回上级菜单)') # 此处询问客户后续操作
while not c_exit_flag:
if choice_p in c.keys():
s_city = '%s省城市列表' % choice_p # 此处打印用户选择的省的城市清单
print(s_city.center(50, '-'))
for i in c[choice_p].keys():
print(i)
print(s_city.center(50, '-'))
choice_c = input('请问您要查看哪个市的列表?(#号键返回上级菜单)') # 此处询问客户后续操作
while not d_exit_flag:
if choice_c in c[choice_p].keys():
s_district = '%s省%s市列表' % (choice_p, choice_c) # 此处打印用户选择的省的城市的区清单
print(s_district.center(50, '-'))
for i in c[choice_p].keys():
print(i)
print(s_district.center(50, '-'))
choice_d = input('请问您要查看哪个区的列表?(#号键返回上级菜单)') # 此处询问客户后续操作
while not t_exit_flag:
if choice_d in c[choice_p][choice_c]:
s_town = '%s省%s市%s区列表' % (choice_p, choice_c, choice_d) # 此处打印用户选择的省的城市的区的镇清单
print(s_town.center(50, '-'))
for i in c[choice_p][choice_c]:
print(i)
print(s_town.center(50, '-'))
choice_d = input('按#号键返回上级菜单,按q键返回至初始菜单') # 此处询问客户后续操作
while not l_exit_flag:
if choice_d == '#':
l_exit_flag = True
elif choice_d == 'q':
print('返回初始菜单成功!')
p_exit_flag = True
else:
print('输入有误,请重新输入,返回上级菜单请输入#号,按q键返回至初始菜单')
elif choice_p == '#':
d_exit_flag = True
else:
print('输入有误,请重新输入,返回上级菜单请输入#号')
elif choice_p == '#':
c_exit_flag = True
else:
print('输入有误,请重新输入,返回上级菜单请输入#号')
elif choice_p == '#':
p_exit_flag = True
else:
print('输入有误,请重新输入,返回上级菜单请输入#号')
else:
print('对不起,当前地域字典为空,请先添加后再查看')
elif choice == 'w':
print()
elif choice == 'q':
print('退出成功')
exit_flag = True
else:
print('对不起,您输入的操作代码有误,请重新输入')
c = {
}
exit_flag = False
exit_flag_2 = False
p_exit_flag = False
c_exit_flag = False
d_exit_flag = False
t_exit_flag = False
l_exit_flag = False
while not exit_flag:
choice_level_1 = input('欢迎来到国家省市区查询系统,按任意键开始查询,退出请输q:')
if choice_level_1 == 'q':
print('系统已退出')
exit_flag = True
else:
if len(c) == 0:
choice_level_2 = input('对不起,当前查询系统数据为空,请您添加后查询,输入任意字符开始添加,退出请输q: ')
if choice_level_2 == 'q':
print('系统已退出')
exit_flag = True
else:
while True:
province_add = input('请您添加省份名称(q返回初始菜单): ')
if province_add == 'q':
break
else:
add_confirm = input('您想要添加的省份名称为:%s,确认请输1,重新输入请输2' % province_add)
if add_confirm == '1':
if province_add not in c.keys():
c[province_add] = {}
print('%s添加成功!' % province_add)
else:
print('对不起,您要添加的省份已经在系统数据中,请勿重复添加,谢谢!')
elif add_confirm == '2':
continue
else:
print('输入有误,请重新输入')