私はPythonには新しく、空白クイズに記入しようとしています。私は別のフォーラムでこのエラーを調べても、自分のコードを修正することはできません。私はこれを行うことができるはずだと思うが、私は答えを見ることができない。エラーは、コードの最後にあるdef game_start関数にあります。ここでrepl.it https://repl.it/LN4r/5 .Thisはエラーです:Python 3.6:リストインデックスは、タプルではなく、整数またはスライスでなければなりません。
Traceback (most recent call last):
File "python", line 62, in <module>
File "python", line 59, in game_start
TypeError: list indices must be integers or slices, not tuple
はここにある私のコードは次のとおりです。
# quizzes below are in order, top to bottom, easy, medium, hard
quizzes = [
['''The keyword def is used to define a ___1___ . A function can be used to replace many instances of a ___2___ in a program. The use of if and ___3___ are helpful in declaring an either/or scenario. When you want a loop to be run until a condition is met, the ___4___ is helpful.'''],
['''When writing Python code, indentation must be ___1___ spaces. Also, a ___2___ must be placed at the end of a function. The ___3___ command can be used to query the user. When using parentheses, double or ___4___ ones can be used.'''],
['''What does "d = {}" create? ___1___\n In this example: "x = {"apple":2}" What is the key? ___2___\n What will: "wheel".replace("e","l") output? ___3___\n list1 = [4,2] What is list1 * 2?(include brackets)___4___''']
]
# answers for all quizzes.
quiz_answers = [
['dictionary', '2', 'whlll', '[4,2,4,2]'],
['four', 'colon', 'input', 'single'],
['function', 'loop', 'else', 'while']
]
# player level selection
def level_chosen():
chosen_level = str(input('Please select a level: easy, medium, hard: '))
if chosen_level == 'easy':
print("You have chosen easy. Let's play!")
return quizzes[0], quiz_answers[0]
elif chosen_level == 'medium':
print("You have chosen medium. Let's play!")
return quizzes[1], quiz_answers[1]
elif chosen_level == 'hard':
print("You have chosen hard. Let's play!")
return quizzes[2], quiz_answers[2]
else:
print('Not a valid option. Try again.')
return level_chosen()
def fill_in_blanks(chosen_level, blank_location = 1, filled_blanks = 0):
blanks = 4
while filled_blanks < blanks:
user_input = input("What is your answer for ___" + str(filled_blanks + 1) + "___?")
if right_answer(chosen_level, blank_location, user_input):
if blank_location >= blanks:
print('You have answered them all correctly!')
show_new_sentence(chosen_level, blank_location)
print('Goodjob! Next blank...')
filled_blanks += 1
blank_location += 1
else:
print('Please try again.')
fill_in_blanks(chosen_level, blank_location, filled_blanks)
def right_answer(chosen_level, blank_location, answer):
return str(answer) == quiz_answers[chosen_level][blank_location - 1]
def show_new_sentence(chosen_level, location):
replace_location = 1
while replace_location <= location:
quizzes[chosen_level] = quizzes[chosen_level].replace('___' + str(replace_location) + '___', quiz_answers[chosen_level][replace_location - 1])
replace_location += 1
print(quizzes[chosen_level])
def game_start():
print('Hello! Welcome to my quiz!')
while True:
chosen_level = level_chosen()
print(quizzes[chosen_level])
fill_in_blanks(chosen_level)
game_start()
何かをデバッグするのに役立つ必要がある場合は、[mcve]を提供し、** minimal **を忘れないでください。 – MSeifert
ここにはたくさんのコードがありますが、リストのインデックスとして使用しているものは、整数またはスライスではなくタプルであると推測しています。ここの暗闇の中でちょうど一発。 –
あなたの 'level_cosen()'メソッドは、 '(quizzes [#]、quiz_answers [#])のタプルを返します。 –