2017-09-18 3 views
-2

저는 파이썬을 처음 사용하고 공백 퀴즈를 채우려 고합니다. 다른 포럼에서 오류를 조사했지만 여전히 내 코드를 수정할 수 없습니다. 나는이 일을 할 수 있어야한다고 생각하지만 나는 그 해답을 볼 수 없다. 오류는 코드 하단의 def game_start 함수에 있습니다. 여기에 repl.it https://repl.it/LN4r/5입니다 동등한 문구 오류입니다 :Python 3.6 :리스트 인덱스는 튜플이 아닌 정수 또는 조각이어야합니다.

# 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() 
+2

디버깅에 도움이 필요한 경우 [mcve]를 입력하고 ** minimal **을 잊어 버리십시오. – MSeifert

+0

여기에 많은 코드가 있는데 여기에는 많은 내용이 없지만 목록의 인덱스로 사용하는 것이 정수 또는 슬라이스가 아닌 튜플이라는 것을 추측하고 있습니다. 여기 어둠 속에서 그냥 쐈어. –

+0

'level_cosen()'메소드는'(퀴즈 [#], quiz_answers [#])의 튜플을 반환합니다. –

답변

0

이와 level_chosen 기능을 교체하십시오 :

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 

여기 것은 내 코드입니다.

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!") 
     l = [quizzes[0], quiz_answers[0]] 
     return l 

    elif chosen_level == 'medium': 
     print("You have chosen medium. Let's play!") 
     l = [quizzes[1], quiz_answers[1]] 
     return l 

    elif chosen_level == 'hard': 
     print("You have chosen hard. Let's play!") 
     l = [quizzes[2], quiz_answers[2]] 
     return l 

    else: 
     print('Not a valid option. Try again.') 
     return level_chosen() 

'level_chosen'함수에서 반환 값이 튜플로 변환됩니다. 이 문제를 해결하려면 "return item1, item2"를 "return list (item1, item2)"또는 "return [item1, item2]"로 바꾸십시오.

튜플은 중복을 포함 할 수없는 가장 간단한 형식의 목록이지만 변경할 수도 없습니다. 튜플은 단순히 두 개체 사이에 쉼표를 넣음으로써 만들어집니다. 유일한 문제는 튜플이 아니라 목록 (편집 가능)을 지정한다는 것입니다. 튜플 주변에 함수 목록()을 추가하면 목록으로 변환됩니다.

+1

모두 감사합니다! Ethan, 당신의 대답은 매우 도움이되었고 나는 그것을 작동 시켰습니다. – 10nry

관련 문제