Python字典存储学生信息(且排序)
由用户输入学生学号与姓名,数据用字典存储,最终输出学生信息(按学号由小到大显示)
students={}
while 1:
student=input("请输入学号(输入q停止输入信息):")
if student=='q':
break#退出
student_name=input("请输入学生姓名:")
students[student]=student_name
#students[student]=student_name
print(students)
list_xh=list(students.keys())
print(list_xh)
list_xh.sort()#用sort()函数排序
for i in list_xh:
print(i,":",students[i])
新章开篇,文件的打开,读写
<Python编程从入门到实战>p142
import sys
filename=sys.argv[0]
f=open(filename,'r',encoding='utf8')
line_no=0
while True:
line_no += 1
line=f.readline()
if line:
print(line_no,":",line)
else:
break
f.close()
<Python编程从入门到实战>p143
import sys
filename=sys.argv[0]
line_no=0
with open(filename,'r',encoding='utf8') as f:
for line in f:
line_no+=1
print(line_no,":",line)
f.close()
文章出处登录后可见!
已经登录?立即刷新