马士兵Python学习笔记_P160_159.实操案例八
一、实操案例八
二、代码示例-显示2019中超联赛前5名排行
# 2、显示2019中超联赛前5名排行
print("--------------------方式一:---------------------")
scores = (("广州恒大", 72), ("北京国安", 70), ("上海上港", 66), ("江苏苏宁", 53), ("山东鲁能", 51))
for index, item in enumerate(scores):
print(index + 1, ".", end=" ")
for i in range(len(item)):
print(item[i], end=" ")
print()
print("--------------------方式二:---------------------")
for index, item in enumerate(scores):
print(index + 1, ".", end=" ")
for score in item:
print(score, end=" ")
print()
运行结果
D:\Environment\Python\Python311\python.exe D:\Environment\PythonWorks\learnPython\马士兵Python\第17章_实操案例\P160_159.实操案例八_2显示2019中超联赛前5名排行.py
--------------------方式一:---------------------
1 . 广州恒大 72
2 . 北京国安 70
3 . 上海上港 66
4 . 江苏苏宁 53
5 . 山东鲁能 51
--------------------方式二:---------------------
1 . 广州恒大 72
2 . 北京国安 70
3 . 上海上港 66
4 . 江苏苏宁 53
5 . 山东鲁能 51
Process finished with exit code 0
三、代码示例-模拟手机通讯录
# 3、模拟手机通讯录
phones = set()
for i in range(5):
info = input(f"请输入第{i+1}个朋友的姓名和手机号码:")
phones.add(info) # 向集合中一次添加1个元素
print(phones)
for item in phones:
print(item)
运行结果
D:\Environment\Python\Python311\python.exe D:\Environment\PythonWorks\learnPython\马士兵Python\第17章_实操案例\P160_159.实操案例八_3模拟手机通讯录.py
请输入第1个朋友的姓名和手机号码:13120231011 张三
请输入第2个朋友的姓名和手机号码:13220231011 李四
请输入第3个朋友的姓名和手机号码:13320231011 王五
请输入第4个朋友的姓名和手机号码:13420231011 马六
请输入第5个朋友的姓名和手机号码:13520231011 罗七
{'13220231011 李四', '13320231011 王五', '13520231011 罗七', '13120231011 张三', '13420231011 马六'}
13220231011 李四
13320231011 王五
13520231011 罗七
13120231011 张三
13420231011 马六
Process finished with exit code 0