May-24-2019, 11:08 PM
I'm making a flashcard program. Right now, I'm storing the questions and answers in separate lists. It works as is, but I want to add the option to study in random order meaning that if I keep the questions and answers in two lists, they're not going to match up with each other once I use random.shuffle. This is the code I have.
Here's a little example.
dict_keys[('question1')]
when all I want is:
question1
#! /usr/bin/python3 import csv from functions import clearScreen clearScreen() # clearing the screen studySet = input("Enter the set you would like to study: ") with open(studySet) as f: # opening the csv file that contains the questions/answers fileContents = csv.reader(f) flashcards = {rows[0]:rows[1] for rows in fileContents} # storing the contents of the csv file into a dictionary questions = [] # empty list to store the questions in answers = [] # empty list to store the answers in for eachKey in flashcards.keys(): # this and next line storing the keys of dict in list called questions questions.append(eachKey) answers.append(flashcards[eachKey]) clearScreen() counter = 0 while counter < len(questions): yourAnswer = input(questions[counter] + "\n") if yourAnswer == answers[counter]: clearScreen() elif yourAnswer != answers[counter]: clearScreen() print("Sorry. That was not correct.") print("The correct answer is " + answers[counter]) questions.append(questions[counter]) answers.append(answers[counter]) counter += 1 clearScreen()I'm trying to instead put the questions/answers in a list of dictionaries.
Here's a little example.
questions = [ { "question1": "answer1", }, { "question2": "answer2", }, { "question3": "answer3", }, ] counter = 0 currentQuestion = questions[counter] myAnswer = input(currentQuestion.keys()) if myAnswer == currentQuestion.values(): print("correct") counter += 1 else: print("wrong")The output I get is:
dict_keys[('question1')]
when all I want is:
question1