Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list and tuple problems
#1
I am on my third day of trying to figure this out. Wall Not sure what else to do. Can someone point me in the right direction please?


This is the error -

Traceback (most recent call last):
File "python", line 67, in <module>
File "python", line 64, in game_start
TypeError: list indices must be integers or slices, not tuple

This is the code-

# 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___, In this example: "x = {"apple":2}" What is the key? ___2___, What will: "wheel".replace("e","l") output? ___3___, list_a = [4,2] What is list_a * 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 = 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()
Reply
#2
chosen level is returning a two item tuple.
you're receiving it as if it were a single item.
That is the cause of the error in line 64
This is what is returned if 'hard' selected:
Output:
['What does "d = {}" create? ___1___, In this example: "x = {"apple":2}" What is the key? ___2___, What will: "wheel".replace("e","l") output? ___3___, list_a = [4,2] What is list_a * 2?(include brackets)___4___'], ['function', 'loop', 'else', 'while']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 470 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,670 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 1,918 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,264 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 88,996 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,669 May-04-2021, 12:06 PM
Last Post: snakes
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,369 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,806 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,571 Jul-29-2020, 12:02 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020