I am trying to code some simple flashcards in Python ( still learning!).
I can read in a text file, split into two lists ( keywords and definitions), find a random keyword (chosenKeyword
) and return both the keyword and its index value from the keyword list BUT when I try to use that index value (which will be exactly the same in the second list as I read them in at the same time line by line) to match to the definition list I keep getting a ValueError
telling me that the item is not in the list (which it is when I manually check). The problem is in my possibleAnswers
function but I cannot work out what it is- any help would be great.
# declare an empty list for answers
answers = []
if keyword.index(chosenKey) == define.index(chosenKey):
answers.append()
else:
pass
# find the matching definition for the keyword and add to the answer list
wrongAnswers = random.sample(define,2)
while define.index(chosenKey) != wrongAnswers:
answers.append(wrongAnswers)
print(answers)
I think You have some issues in displayed code
This is the first one:
if keywords.index(chosenKey) == definitions.index(chosenKey):
As I understand You want to add correct answer to your answer list.
I would do this as follows:
if chosenKey in keywords:
answers.append(definitions[keywords.index(chosenKey)])
else:
pass
second part seems like someone tries to get two random options one of which should be correct answer
wrongAnswers = random.sample(define,2)
while definitions[keywords.index(chosenKey)] not in wrongAnswers:
wrongAnswers = random.sample(define,2)
answers = wrongAnswers
print(answers)