【问题标题】:why does the value of the cell in the array produce 0 when run?为什么运行时数组中单元格的值会产生0?
【发布时间】:2016-06-12 21:13:38
【问题描述】:

为什么SchoolData[1][1](用户名或分数等)总是给出0?

score=0
Class = 0
for x in range (0,3):  # the number of times I want the user to complete the quiz
    print ("Enter your name")
    name = input()
    print ("what class")
    Class = input()

    print("1. 9+10=")
    answer = input()
    answer = int(answer)

    if answer == 19:
        print("correct")
        score = score + 1
    else:
        print("wrong")

    print("2. 16+40=")
    answer = input()
    answer = int(answer)

    if answer == 56:
        print("correct")
        score = score + 1
    else:
        print("wrong")

    print("3. 5+21=")
    answer = input()
    answer = int(answer)

    if answer == 26:
        print("correct")
        score = score + 1
    else:
        print("wrong")

    print("4. 5-6=")
    answer = input()
    answer = int(answer)

    if answer == -1:
        print("correct")
        score = score + 1
    else:
        print("wrong")

    print("5. 21-9=")
    answer = input()
    answer = int(answer)

    if answer == 12:
        print("correct")
        score = score + 1
    else:
        print("wrong")

SchoolData=[[0 for x in range(3)] for x in range(3)]  #this is the array
SchoolData[x][0]=name  #this is the users name enter code here
SchoolData[x][1]=score  #this is the score obtained by the user
SchoolData[x][2]=Class  #this is the users class number
raise SystemExit

我运行代码并完成了 3 次测验。我想知道第一个用户的姓名、分数和班级编号。我尝试从数组中的单元格中提取信息。

【问题讨论】:

  • 我认为您有意图问题, SchoolData=[[...]] 及以下行需要在 for 循环内。您希望 SchoolData 是什么样子?
  • 所以当我输入例如让我说我想知道(当测验由 3 个不同的用户运行 3 次时)第一个用户的姓名或分数或类 1。例如我是我只想通过从数组中提取该信息来了解这个和这个学生的分数和他们的名字。

标签: python loops python-3.x for-loop multidimensional-array


【解决方案1】:

SchoolData[1][1] 为您提供嵌套列表中第二个列表的第二个元素。

SchoolData=[[0 for x in range(3)] for x in range(3)] 会给你输出:[[0,0,0],[0,0,0],[0,0,0]] 我希望这不是你想要的。

在不涉及对象的情况下,您可以快速而肮脏地做自己想做的事情的一种方法是这样(假设您使用的是 python 3.X,基于您对输入的使用):

import collections
school_data = []
questions = ['9+10', '16+40', '5+21', '5-6', '21-9']  

QuizData = collections.namedtuple('QuizData', 'name,class_code,score')
for x in range(0, 5):
    name = input('Enter your name: ')
    class_code = input('Enter class: ')
    score = 0
    for question in questions:
        answer = int(input(question+' = '))
        if answer == eval(question):
            print('Correct!')
            score += 1
        else:
            print('Incorrect.')
    q = QuizData(name, class_code, score)
    school_data.append(q)

此示例在列表 (python docs) 内使用命名元组 (python docs)

要查看第 2 班的最高分,您可以这样做:

highest_scores = sorted([quiz for quiz in school_data if quiz.class_code == '2'], key=lambda q: q.score, reverse=True)

这将返回一个 QuizData 命名元组列表,按从高到低排序,其中 class_code 为“2”。请注意,您已将 class_code 保存为字符串而不是整数。

现在,如果您想在事后访问数据,则需要保存它。一个简单的方法是使用csv 模块。

import csv
with open('quiz_data.csv', 'wb') as csv_file:
    quiz_writer = csv.writer(csv_file, delimiter=',')
    quiz_writer.writerow(['Name', 'Class_Code', 'Score'])  # header row
    quiz_writer.writerows(school_data)

【讨论】:

  • 哦,我认为这是建立一个像表一样的数组,它是一个 3 x 3 表,行上有 3 个用户,用户名、该用户的分数和该用户的类号作为列.
  • 那么,例如作为老师,我将如何获得第二个运行测验的用户的信息,例如他们的分数、姓名或班级编号?我必须输入什么?
  • school_data[1] 将返回列表中第二位的字典;在键值对中。密钥将是 nameclass_codescore
  • 好的,非常感谢,但是如果我运行这个测验 9 次(每班 3 名学生,所以 3 班的)对代码进行一些调整,我会怎么说,例如从最高到最低在特定班级(例如 1 级)的分数?
  • 我已经下课了,我已经更新了我的答案@jazzy,我希望这会有所帮助!
猜你喜欢
  • 1970-01-01
  • 2020-04-19
  • 2014-07-23
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多