【问题标题】:How can I get my 2d list to print after calling the function? in Python [closed]调用函数后如何打印二维列表?在 Python [关闭]
【发布时间】:2021-11-29 18:39:10
【问题描述】:

我已经运行了几次程序并调整了一些东西,但在回答输入问题后仍然没有输出二维列表。 到目前为止,这是我的代码:

def input_test_scores():
    n_students = int(input('The number of students: '))
    n_tests = int(input('The number of tests for each student: '))
    test_scores_list = []
    for student in range(n_students):
        print('Student', student + 1)
        student_list = []
        for tests in range(n_tests):
            print(f'Enter score for test {tests + 1}:')
            student_list.append(int(input()))
        test_scores_list.append(student_list)
    return test_scores_list


input_test_scores()

【问题讨论】:

  • 你希望输出是什么样的?截至目前,代码打印了一个列表列表,其中每个内部列表 i 是学生 i 的分数
  • 输入考试分数后,列表根本没有输出...奇怪
  • 请用想要的输出示例进行说明
  • 你能告诉我它的样子吗?
  • 例如:学生人数=2,测试=1。输入99和100的成绩,输出为[[99], [100]]。如果您希望实际看到返回值,您确实需要打印结果,或者将其保存到一个值中

标签: python list function multidimensional-array


【解决方案1】:

我可能没有理解正确,但最后不就是print(input_test_scores())吗?

【讨论】:

  • 这么简单,哈!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
相关资源
最近更新 更多