Python Forum
Beginner needs a little help with flashcard program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner needs a little help with flashcard program
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creating simplex tableau pivot program easy or difficult for a beginner in Python? alex_0 2 2,535 Mar-31-2021, 03:39 AM
Last Post: Larz60+
  Beginner python password program jakobscheffler 4 5,005 Jan-23-2019, 08:14 PM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

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