示例#1
0
def practice():  #doesnt work
    print('Welcome to the chemical equation practice tool.')
    f = open('samples.txt', 'r')
    samples_list = []
    print('Enter a number between 1 and 5 to fetch a problem to solve')
    sample_search = input()
    for row in f:
        (k, v) = row.split(':', 1)
        if k == sample_search:
            print(v)
            unbalanced = v
            print(
                'Try to balance this equation. Your answer\nshould look something like this\n2H2 + O2 = 2H2O '
            )
            attempt = input()
            equation = Equation(v)
            correct = equation.balance()
            if correct == attempt:
                print('Well done! That is the correct answer!')
                print(
                    'Would you like to try another problem? Enter 1 for yes 2 for no.'
                )
                answer = input()
                if answer == '1':
                    practice()
                if answer == 2:
                    print('Thanks for practicing!')
                    input()
            if equation != attempt:
                print(
                    'Oops... that isn\'t correct.. But heres the correct one...'
                )
                print(correct)

    f.close()
def run_balance():
    """
    Runs the chemical equation balance algorithm
    """
    print('=================================================')
    print(
        'Insert chemical equation with elements in\nparentheses followed by the number of atoms:'
    )
    print('Example: (H)2 + (O)2 = (H)2(O)1')
    user_input = input('>>> ')
    try:
        equation = Equation(user_input)
        print('Balanced equation: ' + equation.balance())
        sleep(3)
        run_balance()
    except IndexError:
        print('Invalid input...')
        sleep(3)
        run_balance()
示例#3
0
def run_balance():
    #prompts user to input a chemical equation and gives them correct format
    print('=================================================')
    print(
        'Insert chemical equation with elements in parentheses followed by the number of atoms:'
    )
    print('Example: (H)2 + (O)2 = (H)2(O)1')
    user_input = input('>>> ')
    #checks to see if the input matches the correct format and runs the balancing function
    try:
        equation = Equation(user_input)
        print('Balanced equation: ' + equation.balance())
        sleep(3)
        run_balance()
    #if input is not correctly formated then it will return as invalid input and ask the player again
    except IndexError:
        print('Invalid input...')
        sleep(3)
        run_balance()
示例#4
0
    async def stoichiometry(self, ctx, element, unknown_element, known_mass: float, mmA: float, mmB: float, *, eq):
        equation = Equation(eq)
        balanced_equation = equation.balance()
        split = balanced_equation.split(' = ')
        left = split[0]
        right = split[1]
        left_components = left.split(' + ')
        right_components = right.split(' + ')
        left_coefs = {}
        right_coefs = {}
        lis = []
        lis2 = []
        u = 0
        u2 = 0
        p = lambda x: enumerate(x)
        o  = list(p(left_components))
        o2  = list(p(right_components))
        for i in range(len(o)):
            lis.append(o[u][0])
            u += 1
        for i in range(len(o2)):
            lis2.append(o[u2][0])
            u2 += 1
        for x in lis:
            if x >= 0:
                left_coefs[left_components[x][1:len(left_components[x]) + 1]] = left_components[x][0]
        for x2 in lis2:
            if x2 >= 0:
                right_coefs[right_components[x2][1:len(right_components[x2]) + 1]] = right_components[x2][0]


        if element in left_coefs.keys(): coefA = int(left_coefs[element])
        elif element in right_coefs.keys(): coefA = int(right_coefs[element])
        else: await ctx.send(left_coefs)

        if unknown_element in left_coefs.keys(): coefB = int(left_coefs[unknown_element])
        elif unknown_element + " " in right_coefs.keys(): coefB = int(right_coefs[unknown_element + ' '])
        else: await ctx.send(right_coefs)

        # Calculation

        mass_to_mass = (known_mass * coefB) * (mmB) / (mmA * coefA)
        await ctx.send(f'{round(mass_to_mass, 2)} grams of {unknown_element}')
示例#5
0
def run_balance():
    """
    Runs the chemical equation balance algorithm
    """
    print('=================================================')
    print(
        'Introduce ecuacion quimica con elementos dentro de \nparentesis seguido por numero de atomos:'
    )
    print('Ejemplo: (H)2 + (O)2 = (H)2(O)1')
    user_input = input('>>> ')
    try:
        equation = Equation(user_input)
        print('Ecuacion balanceada: ' + equation.balance())
        sleep(3)
        run_balance()
    except IndexError:
        print('Input invalido...')
        sleep(3)
        run_balance()
示例#6
0
async def balance(ctx, *, equation):
    """
    Runs the chemical equation balance algorithm
    """
    equation = Equation(equation)
    await ctx.send('Balanced equation: ' + equation.balance())