Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a simple flashcard script - flashcard.py #374

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Flash Card /README.md
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 27 additions & 0 deletions Flash Card /flashcard.py
Original file line number Diff line number Diff line change
@@ -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