首页 > 解决方案 > 列表索引超出范围,但似乎不可能,因为它只是在 3 个问题之后

问题描述

kanji = ['上','下','大','工','八','入','山','口','九','一','人','力','川','七','十','三','二','女',]
reading = ['じょう','か','たい','こう','はち','にゅう','さん','こう','く','いち','にん','りょく','かわ','しち','じゅう','さん','に','じょ']
definition = ['above','below','big','construction','eight','enter','mountain','mouth','nine','one','person','power','river','seven','ten','three','two','woman']

score = number_of_questions = kanji_item = 0

def question_format(prompt_type,lang,solution_selection):
    global reading,definition,score,num_of_questions,kanji_item
    question_prompt = 'What is the '+str(prompt_type)+' for "'+str(kanji[kanji_item])+'"? (Keyboard:'+str(lang)+')\n'
    solution_selection = [reading,definition]
    usr = input(question_prompt)
    if usr in solution_selection[kanji_item] and kanji[kanji_item]:
        score += 1
        num_of_questions += 1
    else:
        pass
    kanji_item += 1

while number_of_questions != 18:
    question_format('READING','Japanese',[0])
print('You got ',score,'/',number_of_questions)

while number_of_questions != 36:
    question_format('DEFINITION','English',[1])
print('You got ',score,'/',number_of_questions)

我过不了大。但我看不出哪里出了问题。我试图改变几乎所有的东西。“kanji_item”应该给出一个共同的索引号,以便答案可以匹配。它毫不费力地解决了前两个问题,但由于某种原因拒绝接受我的第三个问题。

标签: python-3.xlist

解决方案


问题: - 使用错误的名称number_of_questionsvs. num_of_questions - 检查真实性if usr in solution_selection[kanji_item] and kanji[kanji_item]:的错误方法 - 最后一部分总是True因为它是一个非空字符串 - 很多全局变量不被认为是很好的风格

将您的三个列表压缩在一起会更容易,这样您就可以获得 (kanji, reading, description) 的元组,并根据您想要测试的内容将其中的 2 个输入到您的函数中。你这样做了 2 次,一次用于阅读,一次用于描述。

您甚至可以随机化您的元组列表以获得不同的问题“顺序”:

kanji = ['上', '下', '大', '工', '八', '入', '山', '口', '九', '一' , '人', 
         '力', '川', '七', '十', '三', '二', '女',]
reading = ['じょう', 'か', 'たい', 'こう', 'はち', 'にゅう', 'さん', 'こう', 'く',
           'いち', 'にん', 'りょく', 'かわ', 'しち', 'じゅう', 'さん', 'に', 'じょ']
definition = ['above', 'below', 'big', 'construction', 'eight', 'enter', 'mountain', 
              'mouth', 'nine', 'one', 'person', 'power', 'river', 'seven', 'ten', 'three', 
              'two', 'woman']

import random

data = list(zip(kanji, reading, definition))
random.shuffle(data)

def question_format(prompt_type, lang, kanji, solution):
    """Creates a question about *kanji* - the correct answer is *solution*
    Returns 1 if correct else 0."""
    question_prompt = f'What is the {prompt_type} for {kanji}? (Keyboard: {lang})'
    usr = input(question_prompt)
    if usr == solution:
        return 1
    else:
        return 0

questions_asked = 0
correct = 0
for (kanji, reading, _) in data:
    correct += question_format('READING','Japanese', kanji, reading)
    questions_asked += 1

print('You got ',correct,'/',questions_asked)

for (kanji, _, definition) in data: 
    correct += question_format('DEFINITION','ENGLISH', kanji, definition)
    questions_asked += 1

print('You got ',correct,'/',questions_asked)

压缩我们的列表并将它们改组后data看起来像

[('山', 'さん', 'mountain'), ('女', 'じょ', 'woman'), ('力', 'りょく', 'power'), 
('上', 'じょう', 'above'), ('九', 'く', 'nine'), ('川', 'かわ', 'river'), 
('入', 'にゅう', 'enter'), ('三', 'さん', 'three'), ('口', 'こう', 'mouth'), 
('二', 'に', 'two'), ('人', 'にん', 'person'), ('七', 'しち', 'seven'), 
('一', 'いち', 'one'), ('工', 'こう', 'construction'), ('下', 'か', 'below'), 
('八', 'はち', 'eight'), ('十', 'じゅう', 'ten'), ('大', 'たい', 'big')]

推荐阅读