User Tools

Site Tools


wiki:flashcards_beta

FLASHCARDS: Current version = BETA

Click here for the earlier Alpha version.

Ensure the 'questions.txt' and 'answers.txt' files are in the same directory as the Python program.

To Do:

  1. Exception handling - can't find files, mismatched lengths of data files
  2. Add ability to create data files
  3. Add ability to navigate to data files
  4. Add ability to record user's name and (questions studied+length of time) per session
  5. How to merge data from multiple files into one file
  6. First two times running there were 'i is out of range' errors on the 4th out of 5 questions. Fixed?
  7. Streamline code with classes and/or functions
  8. It slices the last character off the file, even if there's no \n character
# FLASHCARDS: A Jeopardy-Style Question Generator
# Eventually enter your name, dates and scores are kept in a file under your name, next time you play it will look you up and welcome you back
# Does it need a function to generate random test questions?
# Eventually start with a choice - would you like to play or would you like to add test questions
# Eventually have the test questions in separate files based on user's choice - new bank of questions, what to call it


from random import randint

# Welcome message
print("Welcome to PYTHON FLASHCARDS: A Jeopardy-Style Question Generator!")
print("All questions and answers come directly from LearnKey's online Python glossary.")
print("I will now load the data from the 'questions.txt' and 'answers.txt' files.  \nPlease ensure they are in the same folder that this program is in.\n\n")

#####################
# Load the data from the questions.txt file into the variable 'contents'
with open("questions.txt", mode='r') as openQuestionFile:
    contents = openQuestionFile.readlines()

# Convert the variable 'contents' into a list called 'question' while removing the \n linebreak character
question = []
for i in contents:
    currentPlace = i[:-1]
    question.append(currentPlace)

# Load the data from the answers.txt file into the variable 'contents'
with open("answers.txt", mode='r') as openAnswerFile:
    contents = openAnswerFile.readlines()

# Convert the variable 'contents' into a list called 'answer' while removing the \n linebreak character
answer = []
for i in contents:
    currentPlace = i[:-1]
    answer.append(currentPlace)

print("Length of question list = ", len(question))
print("Length of answer list = ", len(answer))
yesno = input("Does that look right? yes or no: ")
if (yesno == "yes") or (yesno == "Yes") or (yesno == "y") or (yesno == "Y"):
    print("DATA SUCCESSFULLY LOADED\n\n")
else:
    print("Then expect some trouble...")

print()

# Data is loaded into lists and ready to start the game
#####################

# Game instructions
print("How to play:")
print("I'll give you the answer.")
print("You say the answer outloud in the form of a question: 'What is ...'")
print("Then press <enter> and see if you were right.")
print("Let's get started...")
print()

numberOfQuestions = int(input("How many questions do you want? "))

# How do I stop duplicate questions?
# Maybe with a set?
questionSet = set()
while len(questionSet)<numberOfQuestions:
    questionSet.add(randint(0,len(question)))

# New loop through non-duplicate questions
print("DEBUGGING: There's been a few 'i is out of range' errors, so monitor the loop data...\n\n")
questionNumber = 0
for i in questionSet:
    print(f"Now in a loop around i in questionSet, i={i}, questionSet={questionSet}")
    if questionNumber < 1:
        input("Press enter to start: ")
    else:
        input("Press enter for the next question: ")
    questionNumber = questionNumber + 1
    a = f"Answer: {answer[int(i)]} "
    q = f"What is: {question[int(i)]}"
    print(f"Question #{questionNumber}\n")  
    input(a)
    print(q)
wiki/flashcards_beta.txt · Last modified: 2019/08/19 19:45 by paul