This is a Vocabulary Quiz project build using python Data Structures viz. sets, dictionaries, lists. By running this code it generates a question and four choices in the terminal.
The main aim of this project Vocabulary Quiz using Python Data Structures is to generate a quiz using already given CSV file named 'Vocabulary_list' which contains some words along with their meanings. By giving this 'Vocabulary_list' file we can generate a random question and four options and an exit option.
By selecting any option the system tells us whether we are correct or not.
Let's dive into the project:
Here in this project, we use Data Structures such as dictionaries, lists, sets in python and 'random' library.
Now let's start coding:
1) First, import required libraries. Here we require only 'random' library. We use this function to generate a random number from a particular range.
import random
2) Next, define functions to generate word lists and word dictionaries and processing the given CSV file.
def get_def_and_pop(word_list, word_dict): random_index = random.randrange(len(word_list)) word = word_list.pop(random_index) definition = word_dict.get(word) return word, definition def get_word_and_definition(rawstring): word, definition = rawstring.split(",", 1) return word, definition
3) Next, we need to open the CSV file and then read the file using 'readlines()' function and we can write the file using 'writelines()' function. In this Vocabulary-list file, we have 1st line as Word and Dictionaries so we remove these words by poping using 'pop()' function and by using 'set()' function we can make a CSV file to a set.
fh = open("Vocabulary_list.csv", "r") wd_list = fh.readlines() wd_list.pop(0) wd_set = set(wd_list) fh = open("Vocabulary_set.csv", "w") fh.writelines(wd_set)
4) Next, we make a word dictionary which is word_dict in which 'word' is the key and 'dictionary' is the values for that key.
word_dict = dict() for rawstring in wd_set: word, definition = get_word_and_definition(rawstring) word_dict[word] = definition
5) This is the last part of the code in our project. Here, we make a list of word_dict as wd_list which we use in the later part of code. Next, we define choice_list in which we store our list of choices(definitions). Next, we use 'shuffle()' function from 'random' function as random.shuffle() which shuffles our choices. Next, using 'enumerate()' function we can get the index and choice from choice_list. Next, we ask to enter the input through the keyboard. By entering the digit we get to know whether our option is correct or incorrect.
If we want to get more choices to change the digit in the range() function.
while True: wd_list = list(word_dict) choice_list = [] for x in range(4): word, definition = get_def_and_pop(wd_list, word_dict) choice_list.append(definition) random.shuffle(choice_list) print(word) print("---------------") for idx, choice in enumerate(choice_list): print(idx+1, choice) choice = int(input('Enter your option: 1,2,3 or 4; 0 to exit ')) if choice_list[choice-1] == definition: print("Correct!\n") elif choice == 0: exit(0) else: print("Incorrect")
6) The full code for our project:
import random def get_def_and_pop(word_list, word_dict): random_index = random.randrange(len(word_list)) word = word_list.pop(random_index) definition = word_dict.get(word) return word, definition def get_word_and_definition(rawstring): word, definition = rawstring.split(",", 1) return word, definition fh = open("Vocabulary_list.csv", "r") wd_list = fh.readlines() wd_list.pop(0) wd_set = set(wd_list) fh = open("Vocabulary_set.csv", "w") fh.writelines(wd_set) word_dict = dict() for rawstring in wd_set: word, definition = get_word_and_definition(rawstring) word_dict[word] = definition while True: wd_list = list(word_dict) choice_list = [] for x in range(4): word, definition = get_def_and_pop(wd_list, word_dict) choice_list.append(definition)
random.shuffle(choice_list) print(word) print("---------------") for idx, choice in enumerate(choice_list): print(idx+1, choice) choice = int(input('Enter your option: 1,2,3 or 4; 0 to exit ')) if choice_list[choice-1] == definition: print("Correct!\n") elif choice == 0: exit(0) else: print("Incorrect")
--Thank You--
Submitted by Sridhar Vempati (vempatisridharbzy)
Download packets of source code on Coders Packet
Comments