Beginner needs a little help with flashcard program - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Beginner needs a little help with flashcard program (/thread-18629.html) |
Beginner needs a little help with flashcard program - Chrislw324 - May-24-2019 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. #! /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 RE: Beginner needs a little help with flashcard program - ichabod801 - May-25-2019 I would put them in a list of tuples: cards = [(question1, answer1), (question2, answer2), ...] . Then you can loop through them like this: for question, answer in cards: . Also, you can random shuffle the list, and the answers will stay with the questions.
|