Advertisement
Luninariel

Python Flashcard Program

Sep 12th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. '''This Program will perform the following:
  2.  
  3. * Ask the User to define a specific term from Flashcards.txt
  4. * Collect their answer
  5. * Enter a loop where it will check if the users answer matches the term in Flashcards.txt
  6. * If it does, a counter for number of questions correct increments as well as number of questions answered.
  7. * If it does not, the correct answer is given, and another question is asked.
  8. * If the user enters "exit" as their response, the program quits, and their score is shown
  9. '''
  10. import random
  11. def main():
  12.  
  13.     #Ask the user which flashcard they're studying
  14.     print("What would you like to study today")
  15.     StudyAnswer = input()
  16.  
  17.     if(StudyAnswer.lower() == "programming"):
  18.          #Open the file Programming Flashcards.txt
  19.          f=open("Programming Flashcards.txt")
  20.     elif(StudyAnswer.lower()=="networking"):
  21.          #Open the file Networking Flashcards.txt
  22.          f=open("Networking Flashcards.txt")
  23.     else:
  24.         #force the user to pick a valid card
  25.         print("Please choose a valid subject")
  26.         main()
  27.  
  28.    
  29.     #Read the file flashcards.txt line by line
  30.     flashcards=f.readlines()
  31.  
  32.     #Create A Dictionary to Store the Questions & Answers before we pose them to the user
  33.     FlashDeck = {}
  34.  
  35.     #Split Each Line into Question & Answer based on the ":"
  36.     for line in flashcards:
  37.         SplitCards = line.split(":")
  38.  
  39.         #Set Question & Answer equal to their respective values from the split
  40.         Question = SplitCards[0]
  41.         Answer = SplitCards[1]
  42.  
  43.         #Strip The Leading and Trailing Whitepsace from Questions and Answers
  44.         QuestionStrip = Question.strip()
  45.         AnswerStrip = Answer.strip()
  46.  
  47.         FlashDeck.update({QuestionStrip:AnswerStrip})
  48.  
  49.  
  50.     #Ask the User the definition of a Random term
  51.     Card = random.choice(list(FlashDeck.keys()))
  52.     print("Definition: " + FlashDeck[Card])
  53.     print("\nWhat term does this define?")
  54.  
  55.     #Variable to store a users answer
  56.     UserAnswer = input()
  57.  
  58.     #Variable to count number of questions
  59.     QuestionsTotal = 0
  60.  
  61.     #Variable to count number of Correct Answers
  62.     AnswersCorrect = 0
  63.  
  64.     #While Loop for users input
  65.     while(UserAnswer.strip().lower() != "exit"):
  66.         #Correct answer
  67.         if(UserAnswer.strip().lower() == Card.lower()):
  68.            QuestionsTotal += 1
  69.            AnswersCorrect += 1
  70.            print("Correct!")
  71.            Card = random.choice(list(FlashDeck.keys()))
  72.            print("Definition: " + FlashDeck[Card])
  73.            print("\nWhat term does this define?")
  74.            UserAnswer = input()
  75.            
  76.         else:
  77.             #Incorrect Answer
  78.             print("Incorrect! The correct answer is: " + Card)
  79.             QuestionsTotal += 1
  80.             Card = random.choice(list(FlashDeck.keys()))
  81.             print("Definition: " + FlashDeck[Card])
  82.             print("\nWhat term does this define?")
  83.             UserAnswer = input()
  84.  
  85.  
  86.     if(UserAnswer.strip().lower() == "exit"):
  87.         print("Total Questions Attempted: %d \nTotal Questions Correct: %d" % (QuestionsTotal, AnswersCorrect))
  88.  
  89.         #Ask the user if once they've exited they want to start again
  90.         print("Do you want to start again?")
  91.         UserAnswer = input()
  92.         if(UserAnswer.lower() != "no"):
  93.             main()
  94.         else:
  95.             print("Have a good day")
  96.        
  97.        
  98.  
  99.  
  100.    
  101.    
  102.    
  103.  
  104. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement