Home > Mobile >  How to make a choosing platform using a class object, modules and file handling (use of if else and
How to make a choosing platform using a class object, modules and file handling (use of if else and

Time:05-11

I am writing a code in python which gives basic information about the elements in periodic table when user inputs the symbol of that element.

We have to make it using class object. We can't use multiple classes here to choose from but modules can be made. We can use dictionary, modules and file handling, but use of conditions and switch case isn't allowed.

I have made a separate dictionary for these elemental properties but I want to know how to make it in the way when the user inputs the symbol, it shows the the element's properties.

Here's the library:

def Hydrogen(H):
    i='Hydrogen'
    n=1
    m=1.007
    r=0.12
    ir=0.208
    eng=2.1
def Helium(He):
    i='Helium'
    n=2
    m=4.00260
    r=0.118
    ir='NA'
    eng='NA'
def Lithium(Li):
    i='Lithium'
    n=3
    m=6.941
    r=0.145
    ir=0.06
    eng=1
def Beryllium(Be):
    i='Beryllium'
    n=4
    m=9.01218
    r='NA'
    ir='NA'
    eng=1.5
def Boron(B):
    i='Broron'
    n=5
    m=10.81
    r=0.098
    ir=0.027
    eng=2.0
def Carbon(C):
    i='Carbon'
    n=6
    m=12.11
    r=0.091
    ir="0.26(-4); 0.015( 4)"
    eng=2.5
def Nitrogen(N):
    i='Nitrogen'
    n=7
    m=14.0067
    r=0.092
    ir="0.171 (-3); 0.011( 5); 0.016( 3)"
    eng=3.0
def Oxygen(O):
    i='Oxygen'
    n=8
    m=15.999
    r=0.074
    ir=0.14
    eng=3.5
def Fluorine(F):
    i='Fluorine'
    n=9
    m=18.998403
    r=0.135
    ir="0.135 (-1); 0.007 ( 7)"
    eng=4
def Neon(Ne):
    i='Neon'
    n=10
    m=20.179
    r=0.16
    ir='NA'
    eng='NA'
def Sodium(Na):
    i='Sodium'
    n=11
    m=22.98977
    r=0.196
    ir=0.95
    eng=0.9
def Magnesium(Mg):
    i='Magnesium'
    n=12
    m=24.305
    r=0.16
    ir=0.065
    eng=1.6
def Aluminium(Al):
    i='Aluminium'
    n=13
    m=26.98154
    r=0.143
    ir=0.05
    eng=1.5
def Silicon(Si):
    i='Silicon'
    n=14
    m=28.0855
    r=0.132
    ir="0.271 (-4); 0.041( 4)"
    eng=1.8
def Phosphrus(P):
    i='Phosphorus'
    n=15
    m=30.9738
    r=0.104
    ir=0.034
    eng=2.1
def Sulfur(S):
    i='Sulfur'
    n=16
    m=32.06
    r=0.127
    ir="0.184(-2); 0.029( 6)"
    eng=2.5
def Chlorine(Cl):
    i='Chlorine'
    n=17
    m=35.453
    r=0.127
    ir="0.184(-2); 0.029( 6)"
    eng=3.0
def Argon(Ar):
    i='Argon'
    n=17
    m=39.948
    r=0.192
    ir="NA"
    eng='NA'
def Potassium(K):
    i='Potassium'
    n=19
    m=39.0983
    r=0.235
    ir=0.133
    eng=0.8
def Calcium(Ca):
    i='Calcium'
    n=20
    m=40.08
    r=0.197
    ir=0.099
    eng=1.0
def Scandium(Sc):
    i='Scandium'
    n=21
    m=44.9559
    r=0.161
    ir=0.083
    eng='NA'
def Titanium(Ti):
    i='Titanium'
    n=22
    m=47.88
    r=0.147
    ir='0.09( 2); 0.068( 4)'
    eng=1.5
def Vanadium(V):
    i='Vanadium'
    n=23
    m=50.9414
    r=0.134
    ir='0.074( 3); 0.059( 5)'
    eng=1.6
def Chromium(Cr):
    i='Chromium'
    n=24
    m=51.996
    r=0.127
    ir='0.061( 3); 0.044( 6)'
    eng=1.6
def Manganese(Mn):
    i='Manganese'
    n=25
    m=54.9380
    r=0.126
    ir='0.08( 2); 0.046( 7)'
    eng=1.5
def Iron(Fe):
    i='Iron'
    n=26
    m=55.85
    r=0.126
    ir='0.076( 2); 0.064( 3)'
    eng=1.8
def Cobalt(Co):
    i='Cobalt'
    n=27
    m=58.9332
    r=0.125
    ir='0.078( 2); 0.063( 3)'
    eng=1.8
def Nickel(Ni):
    i='Nickel'
    n=28
    m=58.71
    r=0.124
    ir='0.069( 2); 0.06( 3)'
    eng=1.8
def Copper(Cu):
    i='Copper'
    n=29
    m=63.546
    r=0.128
    ir='0.096( 1); 0.069( 3)'
    eng=1.9
def Zinc(Zn):
    i='Zinc'
    n=30
    m=65.37
    r=0.138
    ir='0.074( 2)'
    eng=1.6
def Gallium(Ga):
    i='Vanadium'
    n=31
    m=69.72
    r=0.161
    ir=0.083
    eng='NA'
def Germanium(Ge):
    i='Germanium'
    n=32
    m=72.64
    r=0.134
    ir='0.074( 3); 0.059( 5)'
    eng=1.6

And the code I will be using to import the dictionary and use to provide the data is:

class Elements:
    def __init__(self, i, n, m, r, ir, eng):
        self.i = i
        self.n = n
        self.m = m
        self.r = r
        self.ir = ir
        self.eng = eng
    def show(self):
        print("\nElemental properties are:\n")
        print("\nName: ", self.i)
        print("\nAtomic Number: ", self.n)
        print("\nAtomic Mass: ", self.m)
        print("\nVanderwaals Radius: ", self.r)
        print("\nIonic Radius: ", self.ir)

f = input("Enter the formula of element to get it's data: ")

I want to know how to call the function by taking the input from the user and get the information in the previous order without using conditional statements like if-else or switch case. If I have to change my whole code that will also work but I want my output that way.

CodePudding user response:

there is a clean way to do it


datas = {"hydrogen":{"element":"Hydrogen","atomic":1,"code":"H","atomic_mass":1.007}}

# you can load data from json file
#with open("datas.json") as Elements:
#   datas = json.load(Elements)

class Element:
    def __init__(self, data):
        self.element    = data['element']
        self.atomic     = data["atomic"]
        self.code       = data["code"]
        self.atomic_mass= data["atomic_mass"]
    def show(self):
        print("\nElemental properties are:\n")
        print("\nName: ", self.element)
        print("\nAtomic Number: ", self.atomic)
        print("\nAtomic Mass: ", self.atomic_mass)
    def __repr__(self):
        # this func is not importent
        return f"<Element({self.__dict__})>"
class Elements(object):
    def __init__(self, name):
        super(Elements, self).__init__()
        self.name = name
        self.data = datas[self.name.lower()] # if element doesn't exist, it will raise KeyError
    def __repr__(self):
        # this func is not importent
        return str(Element(self.data))

try:
    f = input("Enter the formula of element to get it's data: ")
    print("Element is found: ",end="")
    print(Element(Elements(f).data))
except KeyError as e:
    print("element is not found")

i made a class

Elements class take name if element doesn't exist, it will raise KeyError else pass

Element class take the data and show it show func show info

you can put all the data into json file to make the code look pretty

what the code does:

take user input

check if input exist

handle KeyError

show to the user element data

CodePudding user response:

This would probably work. I just turned all your functions into one big dictionary, added it as an instance attribute, and added a method for retrieving the values from the dictionary based on the user input.

class Elements: 
    def __init__(self):  # <---- this is the class constructor
        self.elements = {        # <------ self.elements is dictionary 
            "H": {               # containing all the information about  
                "i": "Hydrogen", # each element in periodic table
                "n": 1,          # I just took the information
                "m": 1.007,      # you had stored in your functions.
                "r": 0.12,       # 
                "ir": 0.208,     
                "eng": 2.1,      # there is one master dictionary
            },                   # and then each element is nested
            "He": {              #  inside it's own sub_dictionary
                "i": "Helium",
                "n": 2,
                "m": 4.00260,
                "r": 0.118,
                "ir": "NA",
                "eng": "NA",
            },
            "Li": {
                "i": "Lithium",
                "n": 3,
                "m": 6.941,
                "r": 0.145,
                "ir": 0.06,
                "eng": 1,
            },
            "Be": {
                "i": "Beryllium",
                "n": 4,
                "m": 9.01218,
                "r": "NA",
                "ir": "NA",
                "eng": 1.5,
            },
            "B": {
                "i": "Broron",
                "n": 5,
                "m": 10.81,
                "r": 0.098,
                "ir": 0.027,
                "eng": 2.0,
            },
            "C": {
                "i": "Carbon",
                "n": 6,
                "m": 12.11,
                "r": 0.091,
                "ir": "0.26(-4); 0.015( 4)",
                "eng": 2.5,
            },
            "N": {
                "i": "Nitrogen",
                "n": 7,
                "m": 14.0067,
                "r": 0.092,
                "ir": "0.171 (-3); 0.011( 5); 0.016( 3)",
                "eng": 3.0,
            },
            "O": {
                "i": "Oxygen",
                "n": 8,
                "m": 15.999,
                "r": 0.074,
                "ir": 0.14,
                "eng": 3.5,
            },
            "F": {
                "i": "Fluorine",
                "n": 9,
                "m": 18.998403,
                "r": 0.135,
                "ir": "0.135 (-1); 0.007 ( 7)",
                "eng": 4,
            },
            "Ne": {
                "i": "Neon",
                "n": 10,
                "m": 20.179,
                "r": 0.16,
                "ir": "NA",
                "eng": "NA",
            },
            "Na": {
                "i": "Sodium",
                "n": 11,
                "m": 22.98977,
                "r": 0.196,
                "ir": 0.95,
                "eng": 0.9,
            },
            "Mg": {
                "i": "Magnesium",
                "n": 12,
                "m": 24.305,
                "r": 0.16,
                "ir": 0.065,
                "eng": 1.6,
            },
            "Al": {
                "i": "Aluminium",
                "n": 13,
                "m": 26.98154,
                "r": 0.143,
                "ir": 0.05,
                "eng": 1.5,
            },
            "Si": {
                "i": "Silicon",
                "n": 14,
                "m": 28.0855,
                "r": 0.132,
                "ir": "0.271 (-4); 0.041( 4)",
                "eng": 1.8,
            },
            "P": {
                "i": "Phosphorus",
                "n": 15,
                "m": 30.9738,
                "r": 0.104,
                "ir": 0.034,
                "eng": 2.1,
            },
            "S": {
                "i": "Sulfur",
                "n": 16,
                "m": 32.06,
                "r": 0.127,
                "ir": "0.184(-2); 0.029( 6)",
                "eng": 2.5,
            },
            "Cl": {
                "i": "Chlorine",
                "n": 17,
                "m": 35.453,
                "r": 0.127,
                "ir": "0.184(-2); 0.029( 6)",
                "eng": 3.0,
            },
            "Ar": {
                "i": "Argon",
                "n": 17,
                "m": 39.948,
                "r": 0.192,
                "ir": "NA",
                "eng": "NA",
            },
            "K": {
                "i": "Potassium",
                "n": 19,
                "m": 39.0983,
                "r": 0.235,
                "ir": 0.133,
                "eng": 0.8,
            },
            "Ca": {
                "i": "Calcium",
                "n": 20,
                "m": 40.08,
                "r": 0.197,
                "ir": 0.099,
                "eng": 1.0,
            },
            "Sc": {
                "i": "Scandium",
                "n": 21,
                "m": 44.9559,
                "r": 0.161,
                "ir": 0.083,
                "eng": "NA",
            },
            "Ti": {
                "i": "Titanium",
                "n": 22,
                "m": 47.88,
                "r": 0.147,
                "ir": "0.09( 2); 0.068( 4)",
                "eng": 1.5,
            },
            "V": {
                "i": "Vanadium",
                "n": 23,
                "m": 50.9414,
                "r": 0.134,
                "ir": "0.074( 3); 0.059( 5)",
                "eng": 1.6,
            },
            "Cr": {
                "i": "Chromium",
                "n": 24,
                "m": 51.996,
                "r": 0.127,
                "ir": "0.061( 3); 0.044( 6)",
                "eng": 1.6,
            },
            "Mn": {
                "i": "Manganese",
                "n": 25,
                "m": 54.9380,
                "r": 0.126,
                "ir": "0.08( 2); 0.046( 7)",
                "eng": 1.5,
            },
            "Fe": {
                "i": "Iron",
                "n": 26,
                "m": 55.85,
                "r": 0.126,
                "ir": "0.076( 2); 0.064( 3)",
                "eng": 1.8,
            },
            "Co": {
                "i": "Cobalt",
                "n": 27,
                "m": 58.9332,
                "r": 0.125,
                "ir": "0.078( 2); 0.063( 3)",
                "eng": 1.8,
            },
            "Ni": {
                "i": "Nickel",
                "n": 28,
                "m": 58.71,
                "r": 0.124,
                "ir": "0.069( 2); 0.06( 3)",
                "eng": 1.8,
            },
            "Cu": {
                "i": "Copper",
                "n": 29,
                "m": 63.546,
                "r": 0.128,
                "ir": "0.096( 1); 0.069( 3)",
                "eng": 1.9,
            },
            "Zn": {
                "i": "Zinc",
                "n": 30,
                "m": 65.37,
                "r": 0.138,
                "ir": "0.074( 2)",
                "eng": 1.6,
            },
            "Ga": {
                "i": "Vanadium",
                "n": 31,
                "m": 69.72,
                "r": 0.161,
                "ir": 0.083,
                "eng": "NA",
            },
            "Ge": {
                "i": "Germanium",
                "n": 32,
                "m": 72.64,
                "r": 0.134,
                "ir": "0.074( 3); 0.059( 5)",
                "eng": 1.6,
            },
        }

    def get_element(self, elem):
        # this is an instance method. The method receives the argument
        # which should be the elemnt symbol
        # the rest I just copy and pasted from your function. except
         # I changes the variables so that they grabbed the appropriate
        # value from the self.elements        
        info = self.elements[elem]      
        print("\nElemental properties are:\n")  
        print("\nName: ", info["i"])
        print("\nAtomic Number: ", info["n"])
        print("\nAtomic Mass: ", info["m"])
        print("\nVanderwaals Radius: ", info["r"])
        print("\nIonic Radius: ", info["ir"])



elements = Elements()  # <---- Initialize the Elements class
symbol = input()       #   Get user input
elements.get_element(symbol)   # <---- call the get_element method with the user input.

when I input 'Ge' this is the output:


Elemental properties are:


Name:  Germanium

Atomic Number:  32

Atomic Mass:  72.64

Vanderwaals Radius:  0.134

Ionic Radius:  0.074( 3); 0.059( 5)
  • Related