Last active
August 22, 2021 20:52
-
-
Save Mathsmaniac/23949d80a26d726db72a50437dff1bf0 to your computer and use it in GitHub Desktop.
Gets the difficulty the user wants to play at and links that with the corresponding elements
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 1 of elements Quiz project - selecting the difficulty | |
Gets the difficulty the user wants to play at | |
And links that with the corresponding elements | |
Written by Nathan Smith | |
05/08/2021""" | |
import ast | |
# Making sure that the input is valid and | |
# getting the values that correspond to the difficulty level selected | |
def checkinput(): | |
while True: | |
difficulty = input("\nPlease enter '1' for easiest, '2' for medium, " | |
"'3' for hard, '4' for very hard, or '5' fo" | |
"r extremely hard: ") | |
try: | |
difficulty = int(difficulty) | |
except ValueError: | |
# Prints "Oops!" and skips the rest of the loop | |
# so that the loop starts again | |
print("Oops!, try again!") | |
continue | |
if MIN_DIFFICULTY <= difficulty <= MAX_DIFFICULTY: | |
break | |
else: | |
print("Oops!, try again!") | |
continue | |
return difficulty | |
# 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() | |
# Making a copy of the dictionary for the elements I'm actually going to use | |
using_dict = my_dict.copy() | |
# Defining some variables and constants | |
counter = 0 | |
MIN_DIFFICULTY = 1 | |
MAX_DIFFICULTY = 5 | |
ALL_ELEMENT_NUMBER = 118 | |
print("Welcome to the elements quiz! A few notes before you start:") | |
print("The symbols (e.g Na) are case sensitive but the names for the" | |
" elements (e.g sodium), are not") | |
print("Difficulty 1 is the first 20 elements, 2 is the first 40, 3 is the fi" | |
"rst 60, 4 is the first 80, and 5 is all of them") | |
print("Ready?") | |
# Gets the difficulty and takes off the required amounts of elements | |
# from the end of the dictionary | |
dif = checkinput() | |
if MIN_DIFFICULTY <= dif <= 4: | |
for i in range(100 - (dif * 20) + 18): | |
using_dict.popitem() | |
counter += 1 | |
else: | |
pass | |
# Print statement to say the amount of elements being used in the current quiz | |
print("The amount of elements being us" | |
"ed is {}.".format(ALL_ELEMENT_NUMBER - counter)) | |
print(using_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment