鱼C论坛

 找回密码
 立即注册
查看: 1245|回复: 4

[已解决]python求助

[复制链接]
发表于 2022-3-18 14:09:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)
最佳答案
2022-3-18 20:37:08
  1. def input_student():
  2.     lst = []
  3.     while True:
  4.         name = input('姓名:')
  5.         if not name:
  6.             break
  7.         age = int(input('年龄:'))
  8.         score = float(input('成绩:'))

  9.         lst.append({'name':name, 'age':age, 'score':score})
  10.         
  11.     return lst

  12. def output_student(L):
  13.     print('+-----------+------+------+')
  14.     print('|    name   | age  | score|')
  15.     print('+-----------+------+------+')
  16.    
  17.     for stu in L:
  18.         print('|{:^11}|{:^6}|{:^6.1f}|'.format(stu['name'], stu['age'], stu['score']))
  19.    
  20.     print('+-----------+------+------+')
  21.    
  22. L = input_student()
  23. output_student(L)
  24. L = L + input_student()# 再添加几个学生
  25. output_student(L)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-18 20:37:08 | 显示全部楼层    本楼为最佳答案   
  1. def input_student():
  2.     lst = []
  3.     while True:
  4.         name = input('姓名:')
  5.         if not name:
  6.             break
  7.         age = int(input('年龄:'))
  8.         score = float(input('成绩:'))

  9.         lst.append({'name':name, 'age':age, 'score':score})
  10.         
  11.     return lst

  12. def output_student(L):
  13.     print('+-----------+------+------+')
  14.     print('|    name   | age  | score|')
  15.     print('+-----------+------+------+')
  16.    
  17.     for stu in L:
  18.         print('|{:^11}|{:^6}|{:^6.1f}|'.format(stu['name'], stu['age'], stu['score']))
  19.    
  20.     print('+-----------+------+------+')
  21.    
  22. L = input_student()
  23. output_student(L)
  24. L = L + input_student()# 再添加几个学生
  25. output_student(L)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-18 23:20:40 | 显示全部楼层
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()

没搞明白为什么L = input_student()不放在def output_student(L):之前还可以使用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-19 19:15:40 | 显示全部楼层
ppp099 发表于 2022-3-18 23:20
def output_student(L):
    print('+-----------+------+------+')
    print('|    name   | age  | sc ...

先有函数 才有你 调用函数。
所以一般 函数成堆全写在前面。 后面调用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-19 23:04:12 | 显示全部楼层
ba21 发表于 2022-3-19 19:15
先有函数 才有你 调用函数。
所以一般 函数成堆全写在前面。 后面调用

懂了,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-19 22:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表