Python 练习实例71

[复制链接]
476|0
 楼主| cooldog123pp 发表于 2022-12-31 18:56 | 显示全部楼层 |阅读模式
题目:编写input()和output()函数输入,输出5个学生的数据记录。
程序分析:无。
程序源代码:
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-

  3. N = 3
  4. #stu
  5. # num : string
  6. # name : string
  7. # score[4]: list
  8. student = []
  9. for i in range(5):
  10.     student.append(['','',[]])

  11. def input_stu(stu):
  12.     for i in range(N):
  13.         stu[i][0] = raw_input('input student num:\n')
  14.         stu[i][1] = raw_input('input student name:\n')
  15.         for j in range(3):
  16.             stu[i][2].append(int(raw_input('score:\n')))

  17. def output_stu(stu):
  18.     for i in range(N):
  19.         print '%-6s%-10s' % ( stu[i][0],stu[i][1] )
  20.         for j in range(3):
  21.             print '%-8d' % stu[i][2][j]

  22. if __name__ == '__main__':
  23.     input_stu(student)
  24.     print student
  25.     output_stu(student)
以上实例输出结果为:
  1. input student num:
  2. 2
  3. input student name:
  4. aaa
  5. score:
  6. 89
  7. score:
  8. 98
  9. score:
  10. 67
  11. input student num:
  12. bbb
  13. input student name:
  14. ccc
  15. score:
  16. 87
  17. score:
  18. 45
  19. score:
  20. 68
  21. input student num:
  22. ddd
  23. input student name:
  24. eee
  25. score:
  26. 56
  27. score:
  28. 78
  29. score:
  30. 56
  31. [['2', 'aaa', [89, 98, 67]], ['bbb', 'ccc', [87, 45, 68]], ['ddd', 'eee', [56, 78, 56]], ['', '', []], ['', '', []]]
  32. 2     aaa      
  33. 89      
  34. 98      
  35. 67      
  36. bbb   ccc      
  37. 87      
  38. 45      
  39. 68      
  40. ddd   eee      
  41. 56      
  42. 78      
  43. 56   





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

本版积分规则

2345

主题

7773

帖子

31

粉丝
快速回复 在线客服 返回列表 返回顶部