The periodic table is a chart that organises all known chemical elements based on their atomic number, electron configurations, and recurring chemical properties. It serves as a fundamental tool in chemistry, physics, and other sciences, helping scientists understand the relationships and behaviours of elements.
For this programming challenge, we are going to search through a list of all 118 elements of the Periodic Table to answer specific queries, generate some statistics and create quizzes. All the information needed for this challenge about the elements of the Periodic Table is stored in a JSON file called periodic-table.json.
JavaScript Object Notation (JSON)
JavaScript Object Notation (JSON) is a lightweight, text-based format for storing and exchanging data that is human and machine-readable. It is a standard format that is often used to transfer/retrieve data from a server using an API. It is used in a wide range of web and mobile applications that rely on accessing/exchanging live data.
A JSON file is like a dictionary data structure. It is made up of two primary parts: keys and values. A key/value pair follows a specific syntax:
- Key: Always a string enclosed in quotation marks
- Value: Can be a string, number, Boolean expression, array, or object
Complex data structure can be created using JSON by combining {dictionaries} and [arrays].
periodic-table.json
Before attempting this challenge you will need to familiarise yourself with the structure of the provided JSON file: periodic-table.json
Within this file, the main key is “elements”. Its value is an array of all the elements of the Periodic Table. Each element is stored as a dictionary with 4 keys:
- Symbol: e.g. H
- Name: e.g. Hydrogen
- Atomic Number: e.g. 1
- Atomic Mass: 1.008
- Phase: Gas
Here is an extract of the periodic-table.json file showing the data for the first three elements: Hydrogen, Helium and Lithium.
{ "elements": [ { "Symbol": "H", "Name": "Hydrogen", "Atomic Number": 1, "Atomic Mass": 1.008, "Phase": "Gas" }, { "Symbol": "He", "Name": "Helium", "Atomic Number": 2, "Atomic Mass": 4.0026022, "Phase": "Gas" }, { "Symbol": "Li", "Name": "Lithium", "Atomic Number": 3, "Atomic Mass": 6.94, "Phase": "Solid" }, ... ] }
Python Code
To be able to read and extract data from our JSON file using Python, we will use the json library. Here is an example of how to use Python code to load the JSON data from the periodic-table.json file. We can then perform a basic linear search to retrieve all the elements in the “Solid” form.
import json # load JSON data from file with open('periodic-table.json','r') as file: data = json.load(file) # Perform a linear search using the JSON data elements = data["elements"] for element in elements: if element["Phase"]=="Solid": print(element["Name"])
You can try and edit this code on our online IDE below:
Your Task:
In the periodic table, the phase of an element refers to its physical state (solid, liquid, or gas) under standard conditions of temperature and pressure (STP), which are typically defined as a temperature of 0°C (273.15 K) and a pressure of 1 atmosphere (atm).
Your task consists of adding extra functions to the above code to perform the following:
-
Generate some statistics to calculate and display the percentage of elements in the Periodic Table which are solid, liquid and gas.
Create a quiz consisting of 10 questions. For each question your program will randomly pick an element from this json file (e.g. Helium) and ask the user to guess its phase (Solid, liquid or gaz?).
Create another quiz where, for each question, your program will randomly pick an element from this json file and display its symbol (e.g. Au) to then ask the user to guess the name of the element (e.g. Gold).
Help…
You can use the following code to randomly pick an element from the periodic table:
import json, random # load JSON data from file with open('periodic-table.json','r') as file: data = json.load(file) # Perform a linear search using the JSON data elements = data["elements"] element = random.choice(elements) print("Randomly picked element: " + element["Name"])