Created
August 22, 2021 20:51
-
-
Save Mathsmaniac/8e7a75edcc2ecf94668b2160e951f9ec to your computer and use it in GitHub Desktop.
Will get the amount of elements corresponding to the difficulty And then ask the question with those, and only those elements in it Will also have the answer for the question stored in a separate variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Component 2 of elements Quiz project - making the question to ask the user | |
Will get the amount of elements corresponding to the difficulty | |
And then ask the question with those, and only those elements in it | |
Will also have the answer for the question stored in a separate variable | |
Written by Nathan Smith | |
05/08/2021""" | |
import ast | |
import random | |
# Function that searches the dict specified | |
# for the value that corresponds to a key. | |
# There is not a built-in function that does this so I had to make it myself | |
def search_dict(value, dictionary): | |
for thing in dictionary: | |
if dictionary[thing] == value: | |
return thing | |
# Raises ValueError if the value is not found in the dict | |
raise ValueError | |
# Opens the file "Elements.txt" and makes it into dictionary form | |
file = open("Elements.txt", "r") | |
contents = file.read() | |
my_dict = ast.literal_eval(contents) | |
file.close() | |
question_element = "" | |
answer_element = "" | |
question = "" | |
QUESTION_AMOUNT = 50 | |
# Making a copy of the dictionary for the elements I'm actually going to use | |
using_dict = my_dict.copy() | |
# Gets the difficulty and takes off the required amounts of elements | |
# from the end of the dictionary | |
dif = int(input("Difficulty? ")) | |
if 1 <= dif <= 4: | |
for i in range(100 - (dif * 20) + 18): | |
using_dict.popitem() | |
else: | |
pass | |
# Runs it 50 times to make sure the | |
# questions are accurate according to the difficulty | |
for i in range(QUESTION_AMOUNT): | |
# If foo is 1, the question will be about the element | |
# (Asking what the element with a particular symbol is) | |
# If it is 2, it's the opposite | |
foo = random.randint(1, 2) | |
# Randomly chooses a symbol from the using_dict | |
bar = random.choice(list(using_dict.values())) | |
if foo == 1: | |
question_element = bar | |
# Searches the dict for the element that corresponds to the symbol | |
answer_element = (search_dict(question_element, my_dict)) | |
question = "What is the name of the element with the chemical " \ | |
"symbol: {}? (" \ | |
"Answer: {})".format(question_element, answer_element) | |
print(question) | |
else: | |
# Same as above, sets the question and answer | |
question_element = (search_dict(bar, using_dict)) | |
answer_element = bar | |
question = "What is the chemical symbol of the " \ | |
"element: {}? (" \ | |
"Answer: {})".format(question_element, answer_element) | |
print(question) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment