Chemistry Problem Solver

# Credits  Name: Abhijith P   Email: abhijithp64@gmail.com
# Compatible with python 3.x.x 

# Program for problems related to chemistry
# 1. Spectroscopy Problems
# 2. Hardness of solution or water

# 3. Calorific value
# 4. Viscosity index 

#   Abhijith P
#   Roll number 2
#
import os
import sys
import math

def clear():#Function for clearing screen
    os.system('cls')

def error(txt):#Function to quit the program by showing an error
    print(txt + "\n\t  Program will terminate now.")
    input("\t  Press ENTER to continue...")
    exit()

def getint(txt):#Function for geeting integer as input. Shows error if input is not int type
    str = input(txt)
    if str.isdigit():
        return int(str)
    else:
        error("\n\tError : Invalid input \"" + str + "\".")
       
def getfloat(txt): #Function for getting float number as input. Shows error if input is not float type
    str = input(txt)
    partition = str.partition('.')
    if str.isdigit():
        return float(str)
    elif (partition[0].isdigit() and partition[1]=='.' and partition[2].isdigit()) or (partition[0]=='' and partition[1]=='.' and partition[2].isdigit()):
        return float(str)
    else:
        error("\n\tError : Invalid input \"" + str + "\".")

#Main function starts from here!
choice = None
while choice !=5:
    choice1 = None
    choice2 = None
    choice3 = None
    clear()
    print("\tProgram for problems related to chemistry")
    print("\t-----------------------------------------")
    print("\n\t1. Spectroscopy Problems\n\t2. Hardness of solution or water\n\t3. Calorific value\n\t4. Viscosity index\n\t5. EXIT")
    choice = input("\n\tEnter your choice : ")
    if choice == "1":
        choice1 = None
        while choice1 != 5:
            clear()
            print("\tSpectroscopy Problems")
            print("\t---------------------")
            print("\n\tAbsorbance")
            print("\n\t1. Calculate Absorbance using intensities of\n\t   incident and transmitted light")
            print("\t2. Calculate Absorbance using concentration,\n\t   cell lenght and molar absorbance coefficient")
            print("\t3. Calculate Absorbance using transmittance")
            print("\n\tTransmittance")
            print("\n\t4. Calculate Transmittance using intensities of\n\t   incident and transmitted light\n\t5. Go to main menu")
            choice1 = input("\n\tEnter your choice : ")
            if choice1 == "1":
                t1 = getfloat("\n\t  Enter intensity of incident light : ")
                t2 = getfloat("\t  Enter intensity of transmitted light : ")
                if t1 <= 0.0 or t2 <= 0.0:
                    error("\n\t  Intensity of incident light or transmitted light cannot be\n\t  zero or negative.")
                print("\t  Absorbance is "+ str(math.log10(t1/t2)) +" .")
                input("\t  Press ENTER to conitnue...")
            elif choice1 == "2":
                conc = getfloat("\n\t  Enter concentration of solution : ")
                x = getfloat("\t  Enter cell length : ")
                coeff = getfloat("\t  Enter molar absorbance coefficient : ")
                if conc < 0.0 or x < 0.0 or coeff < 0.0:
                    error("\n\t  Concentration, cell lenght or molar absorbance coefficient\n\t cannot be negative!")
                print("\t  Absorbance is " + str(coeff*x*conc) + " .")
                input("\t  press ENTER to continue...")
            elif choice1 == "3":
                trans = getfloat("\n\t  Enter transmittance : ")
                if trans < 0:
                    error("\t  Transmitance cannot be negative!")
                print("\t  Absorbance is " + str(-1 * math.log10(trans)) + " .")
                input("\t  Press ENTER to continue...")
            elif choice1 == "4":
                t1 = getfloat("\n\t  Enter intensity of incident light : ")
                t2 = getfloat("\t  Enter intensity of transmitted light : ")
                if t1 <= 0.0 or t2 <= 0.0:
                    error("\n\t  Intensity of incident light or transmitted light cannot be\n\t  zero or negative.")
                print("\t  Transmittance is " + str(t2/t1) + " .")
                input("\t  Press ENTER to continue...")
            elif choice1 == "5":
                choice1 = 5
            else:
                print("\n\tWrong choice")
                input("\tPress ENTER to continue...")
    elif choice == "2":
        choice2 = None
        while choice2 != 4:
            clear()
            print("\tHardness ofsolution or water")
            print("\t----------------------------")
            print("\n\t1. Calculate using normality ")
            print("\t2. Calculate using molarity")
            print("\t3. Calculate using mass")
            print("\t4. Go to main menu")
            choice = str(input("\tEnter your choice : "))
            if choice == "1":
                norm = getfloat("\n\t  Enter normality : ")
                if norm < 0:
                    error("\n\t  Normality cannot be negative!")
                print("\tHardness of " + str(norm)+"N solution is "+ str(norm*50*100)+" mg/l!")
                input("\tPress ENTER to continue...")
            elif choice == "2":
                mol = getfloat("\n\t  Enter molarity : ")
                if mol < 0:
                    error("\n\t  Molarity cannot be negative!")
                print("\tHardness of " + str(mol)+"M solution is "+ str(mol*100*100)+" mg/l!")
                input("\tPress ENTER to continue...")
            elif choice == "3":
                massp = []
                print("\n\t  Note:- Enter zero if their is no ion")
                ph = getint("\t  Enter total number of chloride and sulphide of Ca and Mg : ")
                if ph < 0:
                    error("\n\t  Number of ions cannot be negative!")
                for i in range(ph):
                      massp.append([])
                      m = getfloat("\t  Enter mass of ion " + str(i+1) + " : ")
                      if m <= 0:
                          error("\n\t  Mass cannot be zero or negative!")
                      w = getfloat("\t  Enter molecular weight of ion " + str(i+1) + " : ")
                      if w<=0:
                          error("\n\t  Molecular weight cannot be zero or negative!")
                      massp[i].append(m)
                      massp[i].append(w)
                masst = []
                print("\n\t  Note:- Enter zero if their is no ion")
                th = getint("\t  Enter total number of carbonates and bicarbonates of Ca and Ma : ")
                if th < 0:
                    error("\n\t  Number of carbonates or bicarbonates cannot be negative!")
                for i in range(th):
                      masst.append([])
                      m = getfloat("\tEnter mass of carbonate/bicarbonate " + str(i+1) + " : ")
                      if m <= 0:
                          error("\n\t  Mass cannot be zero or negative!")
                      w = getfloat("\tEnter molecular weight of carbonate/bicarbonate " + str(i+1) + " : ")
                      if w<=0:
                          error("\n\t  Molecular weight cannot be zero or negative!")
                      masst[i].append(m)
                      masst[i].append(w)
                perh = 0.0
                for i in range(len(massp)):
                      perh = perh + ((massp[i][0]*100)/massp[i][1])
                temh = 0.0
                for i in range(len(masst)):
                      temh = temh + ((masst[i][0]*100)/masst[i][1])
                print("\n\tPermamnent hardness is " + str(perh) + " ppm.")
                print("\tTemporary hardness is " + str(temh) + " ppm.")
                print("\tTotal hardness is " + str(perh + temh) + " ppm!")
                input("\tPress ENTER to continue...")
            elif choice == "4":
                choice2 = 4
            else:
                print("\n\tWrong choice!")
                input("\tPress ENTER to continue...")
    elif choice == "3":
        choice3 = None
        HCV = None
        LCV = None
        while choice3 != 3:
            clear()
            print("\tCalorific Value")
            print("\t---------------")
            print("\n\t1. Calculate HCV ")
            print("\t2. Calculate LCV")
            print("\t3. Go to main menu")
            choice3 = input("\n\tEnter your choice : ")
            if choice3 == "1":
                W = getfloat("\n\t  Enter mass of water taken in calorimeter : ")
                if W<=0:
                    error("\n\t  Mass of water cannot be zero or negative!")
                w = getfloat("\t  Enter water equvalent of calorimeter : ")
                if w<=0:
                    error("\n\t  Water equvalent of calorimeter cannot be zero or negative!")
                x = getfloat("\t  Enter mass of fuel taken : ")
                if x<=0:
                    error("\n\t  Mass of fuel cannot be zero or negative!")
                t1 = getfloat("\t  Enter initial temperature of water : ")
                t2 = getfloat("\t  Enter final temperature of water : ")
                cc = getfloat("\t  Enter cooling correction (zero if their is no correction) : ")
                ac = getfloat("\t  Enter acid correction (zero if their is no correction) : ")
                fc = getfloat("\t  Enter fuse correction (zero if their is no correction) : ")
                HCV = ((W+w)*(t2-t1+cc)-(ac+fc))/x
                print("\t  HCV is " + str(HCV))
                input("\t  Press ENTER to continue...")
            elif choice3 == "2":
                if HCV != None:
                    print("\n\t  Last calculated value of HCV is " + str(HCV),"\n")
                    tHCV = float(input("\t  Enter HCV (input -1 to use last calculated value) : "))
                    if tHCV == -1.0:
                        tHCV = HCV
                else:
                    tHCV = float(input("\n\t  Enter HCV : "))
                ph = float(input("\t  Enter percentage of hydrogen : "))
                lhs = float(input("\t  Enter latent heat of steam (-1 to use default value) : "))
                if lhs == -1.0:
                    lhs = 587
                LCV = tHCV-0.09*ph*lhs
                print("\t  LCV is " + str(LCV))
                input("\t  Press ENTER to continue...")
            elif choice3 == "3":
                choice3 = 3
            else:
                print("\n\tWrong choice!")
                input("\tPress ENTER to continue...")
    elif choice == "4":
        choice4 = None
        while choice4 != 2:
            clear()
            print("\tViscosity Index")
            print("\t---------------")
            print("\t1. Calculate viscosity index")
            print("\t2. Go to main menu")
            choice4 = input("\n\tEnter your choice : ")
            if choice4 == "1":
                u = getfloat("\n\t  Enter viscosity of sample oil : ")
                h = getfloat("\t  Enter viscosity of High Viscosity Standard oil : ")
                l = getfloat("\t  Enter viscosity of Low Viscosity Standard oil : ")
                if l == h:
                    error("\t  High viscosity standard oil and low viscosity standard oil cannot have same viscosity!")
                if u < 0 or h < 0 or l < 0:
                    error("\t  Viscosity cannot be zero!")
                print("\t  Viscosity index of sample is " + str(((l-u)/(l-h))*100) + " .")
                input("\t  Press ENTER to continue...")
            elif choice4 == "2":
                choice4 = 2
            else:
                print("\n\tWrong choice!")
                input("\tPress ENTER to continue...")
    elif choice == "5":
        choice = 5
    else:
        print("\n\tWrong choice!")
        input("\tPress ENTER to continue...")


No comments: