Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''This Program will perform the following:
- * Ask the User to define a specific term from Flashcards.txt
- * Collect their answer
- * Enter a loop where it will check if the users answer matches the term in Flashcards.txt
- * If it does, a counter for number of questions correct increments as well as number of questions answered.
- * If it does not, the correct answer is given, and another question is asked.
- * If the user enters "exit" as their response, the program quits, and their score is shown
- '''
- import random
- def main():
- #Ask the user which flashcard they're studying
- print("What would you like to study today")
- StudyAnswer = input()
- if(StudyAnswer.lower() == "programming"):
- #Open the file Programming Flashcards.txt
- f=open("Programming Flashcards.txt")
- elif(StudyAnswer.lower()=="networking"):
- #Open the file Networking Flashcards.txt
- f=open("Networking Flashcards.txt")
- else:
- #force the user to pick a valid card
- print("Please choose a valid subject")
- main()
- #Read the file flashcards.txt line by line
- flashcards=f.readlines()
- #Create A Dictionary to Store the Questions & Answers before we pose them to the user
- FlashDeck = {}
- #Split Each Line into Question & Answer based on the ":"
- for line in flashcards:
- SplitCards = line.split(":")
- #Set Question & Answer equal to their respective values from the split
- Question = SplitCards[0]
- Answer = SplitCards[1]
- #Strip The Leading and Trailing Whitepsace from Questions and Answers
- QuestionStrip = Question.strip()
- AnswerStrip = Answer.strip()
- FlashDeck.update({QuestionStrip:AnswerStrip})
- #Ask the User the definition of a Random term
- Card = random.choice(list(FlashDeck.keys()))
- print("Definition: " + FlashDeck[Card])
- print("\nWhat term does this define?")
- #Variable to store a users answer
- UserAnswer = input()
- #Variable to count number of questions
- QuestionsTotal = 0
- #Variable to count number of Correct Answers
- AnswersCorrect = 0
- #While Loop for users input
- while(UserAnswer.strip().lower() != "exit"):
- #Correct answer
- if(UserAnswer.strip().lower() == Card.lower()):
- QuestionsTotal += 1
- AnswersCorrect += 1
- print("Correct!")
- Card = random.choice(list(FlashDeck.keys()))
- print("Definition: " + FlashDeck[Card])
- print("\nWhat term does this define?")
- UserAnswer = input()
- else:
- #Incorrect Answer
- print("Incorrect! The correct answer is: " + Card)
- QuestionsTotal += 1
- Card = random.choice(list(FlashDeck.keys()))
- print("Definition: " + FlashDeck[Card])
- print("\nWhat term does this define?")
- UserAnswer = input()
- if(UserAnswer.strip().lower() == "exit"):
- print("Total Questions Attempted: %d \nTotal Questions Correct: %d" % (QuestionsTotal, AnswersCorrect))
- #Ask the user if once they've exited they want to start again
- print("Do you want to start again?")
- UserAnswer = input()
- if(UserAnswer.lower() != "no"):
- main()
- else:
- print("Have a good day")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement