首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么数组中的单元格的值在运行时会生成0?

为什么数组中的单元格的值在运行时会生成0?
EN

Stack Overflow用户
提问于 2016-03-01 01:54:56
回答 1查看 49关注 0票数 1

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

代码语言:javascript
复制
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次测验。我想知道第一个用户的名字,分数和班号。我尝试从数组中的单元格中提取信息。

EN

回答 1

Stack Overflow用户

发布于 2016-03-01 02:10:38

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

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

在不使用对象的情况下,有一种方法可以快速有效地完成你想做的事情(假设你基于对input的使用来使用python 3.X ):

代码语言:javascript
复制
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班的最高分数,您可以这样做:

代码语言:javascript
复制
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模块。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35706198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档