jeudi 14 juillet 2016

Cannot access a list by index that was randomly chosen from a list of lists

I am working on a python learning exercise that requires creating a text game run from the console. I want to create a trivia game. (Yes, it's Harry Potter trivia - please don't judge) To make the question pool, I have made a text file of the questions, answer options, and answers. To keep the correct options and answers with the question when they are randomly chosen, I am using a list of lists. But when I pull one of the lists out of the list of lists and assign it to a variable, accessing it by index pulls the character at the index, not the list item.

Here is the example data in the text file, named questions.txt:

['What was the famous Albus Dumbledore\'s full name?','Albus Merlin Baowulf Roderick Dumbledore','Albus Cato Eleret Scholasticus Dumbledore','Albus Percival Wulfric Brian Dumbledore',3]
['What fruit must you tickle to get into the Hogwarts kitchen?','Pear','Apple','Quince',1]
['What is the name of the French wizarding school?','Acadamie de Magique','Beauxbatons','Fontainebleu',2]
['What is the name of Mr. Filch\'s cat?','Mr. Pinky Paws','Crookshanks','Mrs. Norris',3]

the first item in each list is the question, the second, third, and fourth are the answer options, and the final integer is the number of the correct answer. I import this into my python file and use splitlines so that each line in the text file becomes an item in the list, thus a list of lists.

with open('questions.txt', 'r') as infile:
   questions = infile.read()

my_questions = questions.splitlines()

I tested it at this point by printing something from my_questions by the index, and it worked.

print my_questions[0]

resulted in

['What was the famous Albus Dumbledore\'s full name?','Albus Merlin Baowulf Roderick Dumbledore','Albus Cato Eleret Scholasticus Dumbledore','Albus Percival Wulfric Brian Dumbledore',3]

So I did the next step of choosing one of the lists randomly from the list and assigning it to a new variable. I created a function (I made sure I had import random at the top of the file):

def question():
        quest = random.choice(my_questions)

        print quest[0]

And got only a [. I.e. the first character, not the first string.

So I hard coded a question and answer list and tried the same test, and it worked. The code:

def question():
        quest = random.choice(my_questions)
        quest1 = ['What is the name of Mr. Filch\'s cat?','Mr. Pinky Paws','Crookshanks','Mrs. Norris',3]
        print quest
        print quest[0]
        print quest1
        print quest1[0]

and the result was:

PS C:\Users\Katrina\temp> python hogwarts.py
['What fruit must you tickle to get into the Hogwarts kitchen?','Pear','Apple','Quince',1]
[
["What is the name of Mr. Filch's cat?", 'Mr. Pinky Paws', 'Crookshanks', 'Mrs. Norris', 3]
What is the name of Mr. Filch's cat?

I'm sure there is something I am missing here...




Aucun commentaire:

Enregistrer un commentaire