-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay.py
More file actions
78 lines (58 loc) · 3.39 KB
/
play.py
File metadata and controls
78 lines (58 loc) · 3.39 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import csv
import json
# 2 Create a spaced repetition vocabulary learning game. The user will be given words from the CSV list and progress through them by typing the correct definition response. When they answer incorrectly it will be game over and they will be given a high score. They will then have to start from the beginning to play again forcing them to continue practicing.
# 2.1 Create the files to read both the long list of space-repeated words from the CSV file and the word definitions from the definitions.json file. We can loop over the CSV list to test the user continuously until they fail, at which point give them a score. The json file will be used to give the user a word definition if it's their first time encountering the word. (also check their answer against correct definition)
def load_words_from_csv(file_path):
#loads a list of words from a csv file
with open(file_path, 'r') as f:
reader = csv.reader(f)
words = []
for row in reader:
if row:
words.append(row[0])
return words
def load_definitions_from_json(file_path):
#load definitions from json file
with open(file_path, 'r') as f:
definitions = json.load(f)
return definitions
# 2.2 Define the main game loop, testing the user on each word in the CSV list one after another in the spaced repetition algorithm order. This while loop will exit only when each word has been mastered or the user makes a mistake.
def test_user(words, definitions):
# Test the user on the definitions of the words provided.
high_score = 0
encountered_words = set() # Track which words the user has already encountered, set removes duplicates in array.
for word in words:
print(f'\nDefine: {word}')
# If we have a word that isn't defined, skip it - catches any errors in definitions JSON
if word not in definitions:
print('\nDefinition not found for this word. Skipping...')
# Confirmation word has definition, so we can test the user
# Returns value on key of 'word' for definition
definition = definitions[word]
# If it's the first time encountering the word, show the definition
if word not in encountered_words:
print(f'\n{word} -> \"{definition}\"')
user_input = input('\nAnswer: ')
# Check if the user input matches the definition
if user_input.lower() != definition.lower():
print('\nGame Over!')
print(f'High Score: {high_score}')
print(f'Correct Definition: {definition}')
return
# Add word to encountered words and increment the score as they answered correctly
encountered_words.add(word) #Adds to set
high_score += 1
print('Correct!')
# We have finished looping through all words in the CSV file, the user has won the game and mastered all definitions.
print('\nCongratulations! You have completed the game! (:)')
print(f'\nHigh Score: {high_score}')
if __name__ == "__main__": # Confirms we are running this file specifically
# File paths
csv_file_path = "words_history.csv"
json_file_path = "definitions.json"
# Load data
words = load_words_from_csv(csv_file_path)
definitions = load_definitions_from_json(json_file_path)
# Start the game
print("Welcome to the Word Definition Game!")
test_user(words, definitions)