|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
写一个程序,任意输入n个学生的信息,形成字典后存于列表中 input_studet()
学生的信息包括:姓名(字符串),年龄(整数),成绩(浮点数)
循环输入学生信息,知道输入学生姓名为空时结束输入,
最后形成的字典:L=[{"name":"jack","age":19,"score":88.9},{"name":"rouse","age":20,"score":82.3},...]
将上述列表显示为如下的表格(打印) output_stunt()
+-----------+------+------+
| name | age | score|
+-----------+------+------+
| jack | 19 | 88.9|
| rouse | 20 | 82.3|
.........
+-----------+------+------+
def input_student():
'''要求用户循环输入学生信息,将每个学生的信息保存到字典中,并将字典存于列表中,并返回列表'''
def output_student():
'''按照如上格式输出学生信息'''
L = input_student()
output_student(L)
L = L + input_student()# 再添加几个学生
output_student(L)
- def input_student():
- lst = []
- while True:
- name = input('姓名:')
- if not name:
- break
- age = int(input('年龄:'))
- score = float(input('成绩:'))
- lst.append({'name':name, 'age':age, 'score':score})
-
- return lst
- def output_student(L):
- print('+-----------+------+------+')
- print('| name | age | score|')
- print('+-----------+------+------+')
-
- for stu in L:
- print('|{:^11}|{:^6}|{:^6.1f}|'.format(stu['name'], stu['age'], stu['score']))
-
- print('+-----------+------+------+')
-
- L = input_student()
- output_student(L)
- L = L + input_student()# 再添加几个学生
- output_student(L)
复制代码
|
|