## [Equation solver] Error should occur and doesn't ???

This is the place for queries that don't fit in any of the other categories.

### [Equation solver] Error should occur and doesn't ???

Hi all!

I've been working on an Equation Solver. Until now, it has worked well, although it had some bugs a crashes that I fixed, some times with help from some members of this forum, since I'm relatively new to Python and this is my first true project. Btw, I already created a page on the Completed scripts section, since the script is almost fully functional (http://python-forum.org/viewtopic.php?f=11&t=4253).

Now, the real problem is that, when solving 2nd degree equations, you may type the value you wish to assign to "a", "b" and "c". However, if you don't type a number in, let's say, "a", it'll give you an error and let you start doing more equations. In this menu, you may only chose 1st or 2nd, but if you type anything besides that, it'll pass and ask you for the next value. I know I'm not explaining very well, so I'll just leave you with my results and the full code to let you test it.

bug:
Code: Select all
`Welcome to:+--------------------+|EQUATION SOLVER V0.3|+--------------------+by: Luís Ferreira- MAIN MENU -What do you want to do?[1] Start solving equations[2] More info[3] Quit:: 1Equations degree (1st or 2nd): 2ndYour equation is, in the canonical form: a*x^2 + b*x + c = 0Assign a value to "a": asdYou must type only numbers and math symbols!Equations degree (1st or 2nd): 2ndYour equation is, in the canonical form: a*x^2 + b*x + c = 0Assign a value to "a": asdYou must type only numbers and math symbols!Equations degree (1st or 2nd): sadAssign a value to "b": `

full code:
Code: Select all
`import mathimport timeprint('''Welcome to:+--------------------+|EQUATION SOLVER V0.3|+--------------------+by: Luís Ferreira''')def continue1choice():    continue1 = input('Do you want to solve more equations? (y/n) ')    if continue1 == 'y':        EquationSolver()    elif continue1 == 'n':        print('Thank you for checking my app!')    elif continue1 != 'y' and continue1 != 'n':        print('You must chose "y" (yes) or "n" (no)!')        continue1choice()def EquationSolver():    EquationType = input('Equations degree (1st or 2nd): ')    if EquationType == '1st':        eq = input('Type your equation here (use "x" as variable): ')        try:            eq1 = eq.replace("=","-(")+")"            end = eval(eq1,{'x':1j})            pass        except SyntaxError:            print('You must write a vallid equation!')            continue1choice()        eq1 = eq.replace("=","-(")+")"        end = eval(eq1,{'x':1j})        print(-end.real/end.imag)        continue1choice()    elif EquationType == '2nd':        solve2()        continue1choice()def solve2():    print('Your equation is, in the canonical form: a*x^2 + b*x + c = 0')    a = input('Assign a value to "a": ')    try:        int(a)        pass    except ValueError:        print('You must type only numbers and math symbols!')        EquationSolver()    b = input('Assign a value to "b": ')    try:        int(b)        pass    except ValueError:        print('You must type only numbers and math symbols!')        EquationSolver()    c = input('Assign a value to "c": ')    try:        int(c)        pass    except ValueError:        print('You must type only numbers and math symbols!')        EquationSolver()    delta = b**2 - 4*a*c    if delta < 0:        print('The equation is impossible - it has 0 solutions.')        continue1choice()    elif a == 0 and b == 0:        print('The equation is impossible - it has 0 solutions.')        continue1choice()    elif delta == 0:        x = -b        print('Your equation has 1 solution, which is %r.' % r)        continue1choice()    elif delta > 0:        pass    sqrtDelta = math.sqrt(delta)    x1 = (-b + sqrtDelta)/2*a    x2 = (-b - sqrtDelta)/2*a    print('Your equation has 2 solutions, which are %r and %r.' %(x1, x2))def MainMenu():    print('- MAIN MENU -')    print('What do you want to do?')    print('''[1] Start solving equations[2] More info[3] Quit''')    option1 = input(':: ')    if option1 == '1':        EquationSolver()    elif option1 == '2':        print('''- More info -This small app, Equation Solver, was totally made by me,Luís Ferreira, in order to use it for educational purpo-ses for me (to start working better with Python) and forothers too, as a suplement to their education in Math. Ihope that you comprehend that there maybe many mistakeswith the code, since I'm relativly new to Python and sin-ce this project is still in a very fresh fase. However,I don't want to make this like Sympy or other equationsolvers - although I used some useful tips and code avai-lable on the internet. I hope you enjoy it and let me knowabout any mistakes or sugestions.Thank you for trying it!''')        input('Press [enter] to continue')        print('')        MainMenu()    elif option1 == '3':        print('Thank you for checking my app!')        time.sleep(3)    elif option1 != '1' and option1 != '2' and option1 != '3':        print('You must chose an available option!')        MainMenu()MainMenu()`

Anyway, is there any advice you can give to me for solving this? Any idea?

Best regards,
Luis Ferreira.
luis.simao.af98

Posts: 18
Joined: Thu May 16, 2013 8:37 pm

### Re: [Equation solver] Error should occur and doesn't ???

Your problem is that your equationsolver function does not have an else clause to handle the case of any other input than '1st' or '2nd'.
When something else is input, the function just asks for the input and then does nothing with it.

Which means that, in the following piece of code, when Equationsolver() is called and it's given a different value than '1st' or '2nd', nothing happens, and the code goes on to the next line. Which is 'b = input('Assign a value to "b": ')
Code: Select all
`def solve2():    print('Your equation is, in the canonical form: a*x^2 + b*x + c = 0')    a = input('Assign a value to "a": ')    try:        int(a)        pass    except ValueError:        print('You must type only numbers and math symbols!')        EquationSolver()    b = input('Assign a value to "b": ')`
hansn

Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

### Re: [Equation solver] Error should occur and doesn't ???

Sorry for the late reply, but I hadn't been able to access the forum on the last wee, idk why... Anyway, I figured that out and I should recheck my code more often before I post it here
Anyway, thanks a lot for your help! I've corrected some other minor errors, mistakes and bugs and now it should work perfectly...

Here it is the complete code:
Code: Select all
`import mathimport timeimport sysprint('''Welcome to:+--------------------+|EQUATION SOLVER V0.3|+--------------------+by: Luís Ferreira''')def continue1choice():    continue1 = input('Do you want to solve more equations? (y/n) ')    if continue1 == 'y':        EquationSolver()    elif continue1 == 'n':        print('Thank you for checking my app!')        time.sleep(3)        sys.exit()    elif continue1 != 'y' and continue1 != 'n':        print('You must chose "y" (yes) or "n" (no)!')        continue1choice()def EquationSolver():    EquationType = input('Equations degree (1st or 2nd): ')    if EquationType == '1st':        eq = input('Type your equation here (use "x" as variable): ')        try:            eq1 = eq.replace("=","-(")+")"            end = eval(eq1,{'x':1j})            pass        except SyntaxError:            print('You must write a vallid equation!')            continue1choice()        eq1 = eq.replace("=","-(")+")"        end = eval(eq1,{'x':1j})        result = -end.real/end.imag        if result == -0.0:            result = 0        print('The solution for your equation is: %r' % result)        continue1choice()    elif EquationType == '2nd':        print('Your equation is, in the canonical form: a*x^2 + b*x + c = 0')        a = input('Assign a value to "a": ')        try:            int(a)            pass        except ValueError:            print('You must type only numbers and math symbols!')            EquationSolver()        a = int(a)        b = input('Assign a value to "b": ')        try:            int(b)            pass        except ValueError:            print('You must type only numbers and math symbols!')            EquationSolver()        b = int(b)        c = input('Assign a value to "c": ')        try:            int(c)            pass        except ValueError:            print('You must type only numbers and math symbols!')            EquationSolver()        c = int(c)        delta = b**2 - 4*a*c        if delta < 0:            print('The equation is impossible - it has 0 solutions.')            continue1choice()        elif a == 0 and b == 0:            print('The equation is impossible - it has 0 solutions.')            continue1choice()        elif delta == 0:            x = -b/(2*a)            print('Your equation has 1 solution, which is %r.' % x)            continue1choice()        elif delta > 0:            pass        sqrtDelta = math.sqrt(delta)        x1 = (-b + sqrtDelta)/(2*a)        x2 = (-b - sqrtDelta)/(2*a)        print('Your equation has 2 solutions, which are %r and %r.' %(x1, x2))        continue1choice()    elif EquationType != '1st' and EquationType != '2nd':        print('You must chose only "1st" or "2nd"!')        EquationSolver()def MainMenu():    print('- MAIN MENU -')    print('What do you want to do?')    print('[1] Start solving equations')    print('[2] More info')    print('[3] Quit')    option1 = input(':: ')    if option1 == '1':        EquationSolver()    elif option1 == '2':        print('''- More info -This small app, Equation Solver, was totally made by me,Luís Ferreira, in order to use it for educational purpo-ses for me (to start working better with Python) and forothers too, as a suplement to their education in Math. Ihope that you comprehend that there maybe many mistakeswith the code, since I'm relativly new to Python and sin-ce this project is still in a very fresh fase. However,I don't want to make this like Sympy or other equationsolvers - although I used some useful tips and code avai-lable on the internet. I hope you enjoy it and let me knowabout any mistakes or sugestions.Thank you for trying it!''')        input('Press [enter] to continue')        print('')        MainMenu()    elif option1 == '3':        print('Thank you for checking my app!')        time.sleep(3)        sys.exit()    elif option1 != '1' and option1 != '2' and option1 != '3':        print('You must chose an available option!')        MainMenu()MainMenu()`

Cheers!
luis.simao.af98

Posts: 18
Joined: Thu May 16, 2013 8:37 pm