-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (52 loc) · 1.92 KB
/
main.py
File metadata and controls
64 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
class FlashcardApp:
def __init__(self):
self.flashcards = []
self.load_flashcards()
def load_flashcards(self):
try:
with open('flashcards.json', 'r') as file:
self.flashcards = json.load(file)
except FileNotFoundError:
self.flashcards = []
def save_flashcards(self):
with open('flashcards.json', 'w') as file:
json.dump(self.flashcards, file)
def add_flashcard(self):
question = input("Enter the question: ")
answer = input("Enter the answer: ")
self.flashcards.append({'question': question, 'answer': answer})
self.save_flashcards()
print("Flashcard added!")
def quiz(self):
if not self.flashcards:
print("No flashcards available. Please add some first.")
return
score = 0
for flashcard in self.flashcards:
user_answer = input(f"Q: {flashcard['question']} ")
if user_answer.strip().lower() == flashcard['answer'].strip().lower():
print("Correct!:)")
score += 1
else:
print(f"Incorrect, the correct answer is: {flashcard['answer']}")
print(f"Your final score is: {score}/{len(self.flashcards)}")
def run(self):
while True:
print("\nFlashcard Quiz App")
print("1. Add Flashcard")
print("2. Take Quiz")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == '1':
self.add_flashcard()
elif choice == '2':
self.quiz()
elif choice == '3':
print("Thank you, keep studying hard! Byebye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
app = FlashcardApp()
app.run()