diff --git a/Flash Card /README.md b/Flash Card /README.md new file mode 100644 index 0000000..8adf9b4 --- /dev/null +++ b/Flash Card /README.md @@ -0,0 +1,20 @@ +## Prerequisites ++ Python 3.x ++ Import of random module + +## How the code works ++ Imports random module --> Selective import can be done of random.choice() only ++ ++ The code uses both for and while loops ++ ++ The while loop is infinite, and option to exit the program is given to user ++ ++ User enters question and answer which is accepted via an input() ++ ++ These input question-answers are stored in a dictionary as key-value pairs (key = question, value = answer) ++ ++ After input by the user, stored questions are randomly asked to user using random.choice() from random module ++ ++ Answers are verified by calling the question stored in the dictionary ++ ++ Code ends when user inputs "n" to indicate that they wish to exit, or when every question has been asked. diff --git a/Flash Card /flashcard.py b/Flash Card /flashcard.py new file mode 100644 index 0000000..09a8fcf --- /dev/null +++ b/Flash Card /flashcard.py @@ -0,0 +1,27 @@ +import random +num_questions = int(input("Enter number of questions: ")) +q_solutions = dict() # creates an empty dictionary +count = 0 # counts the number of questions asked +for i in range (num_questions): + question = input("Enter your question: ") + answer = input("Enter your answer: ") + q_solutions[question.lower()] = answer.lower() # adding the key-value pair to dictionary + print("\n") +q_s = list(q_solutions.keys()) +while True: + q = random.choice(q_s) # picks a random key from the question and answer dictionary + count+=1 # increments the count variable, to indicate that a question has been asked + print(q) + ans = input("Enter an answer for the above question: ") + if ans.lower() == q_solutions[q]: + print("You're right!") + else: + print("You're wrong! \nThe correct answer is:",q_solutions[q]) + q_solutions.pop(q) # removes the question-answer pair that has been asked to prevent repetition + if count > len(q_solutions): + print("All questions have been asked.") + break + choice = input("\nDo you wish to continue? Enter Y OR N: ") + print() + if choice.lower() == 'n': + break