0

Wrote this for my college open evening just to give prospective students a taste of python. added a little "information" section just to bulk it out a bit (this section is slightly messy i know). Not sure if it works in command line, definitely works in IDLE but the command line is outdated on the college machines so bugs such as the trailing return (/r on the end of inputs) still apply so it is hard to validate.

Hope this is helpful for beginners etc (commented in places so prospective students can at least understand some of what's going on)

```
#import required library
import math
def solver(a,b,c):
###############################################################
### this function is used to solve the quadratic equation ###
### and includes the mathematics required to do so properly ###
###############################################################
#solve the discriminant and square root it
discriminant = (b*b)-(4*a*c)
rootedDiscriminant = math.sqrt(discriminant)
#solve for both solutions of x
solution1 = ((-b)+rootedDiscriminant)/(2*a)
solution2 = ((-b)-rootedDiscriminant)/(2*a)
#decides whether equation has 1 or 2 roots
if solution1 == solution2:
answer = [solution1]
else:
answer = [solution1,solution2]
#returns the answer to the main program
return answer
def menu():
#################################################
### displays the main menu and asks for input ###
#################################################
#sets a loop so that the menu reappears after each use
exitprogram = False
while exitprogram == False:
#Displays the main menu
print(">>>>>-----------------------------------<<<<<")
print(">>>>> QUADRATIC EQUATION SOLVER <<<<<")
print(">>>>>-----------------------------------<<<<<")
print("")
print("Please select an option from the menu below")
print("")
print("1. Solve a quadratic equation of the form ax^2 + bx + c = 0")
print("2. Learn about quadratic equations and their uses in industry, mathematics and science")
print("3. Exit")
print("")
#Asks for an input and validates it to prevent crashing from unknown input types (i.e. string, decimal)
accepted = False
while accepted == False:
try:
choice = int(input("Please select an option using its corresponding number (e.g. 3) >>> "))
accepted = True
except:
print("")
print("INVALID INPUT. PLEASE ENTER ONE INTEGER ONLY")
print("")
accepted = False
#Menu accepts input for choice 1 and validates it (solving an equation)
if choice == 1:
validinputs = False
while validinputs == False:
try:
print("")
print(""">>>>------------------------------------------------------------------------------<<<<""")
print(""">>>> IMPORTANT: Please make sure your equation is of the form "ax^2 + bx + c = 0" <<<<""")
print(""">>>>------------------------------------------------------------------------------<<<<""")
print("")
print("")
print("""Now please have ready your values for "a" "b" and "c" """)
print("")
number1 = eval(input("""Please enter your value for "a" >>> """))
number2 = eval(input("""Please enter your value for "b" >>> """))
number3 = eval(input("""Please enter your value for "c" >>> """))
validinputs = True
try:
#trys to call the function to solve the equation with given values.
#If the solver can find the answer to the problem then it will return
#either one or two values in a list (dependant on whether the equation
#has one or two distinct real roots. If the solver cannot find the answer
#it skips to the "except" section below
problemsolution = solver(number1,number2,number3)
if len(problemsolution) == 2:
for each in problemsolution:
print("")
print("One root of the equation is ",each)
else:
print("")
print("The equation has one real root: ",problemsolution[0])
print("")
exitloop = False
while exitloop == False:
print("")
selection = input("Would you like to return to the main menu (y/n)? >>> ")
if selection == "n":
exit()
elif selection == "y":
exitloop = True
print("")
print("----------------------------------------------------------")
print("")
else:
print("INVALID OPTION")
except:
#If the function crashes upon running then it must be trying
#to calculate a non-real number (in this case the program will be trying
#to compute the square root of a negative number. This gives an imaginary
#number and will crash the function. We can use this and realise the equation
#must have no real roots, and use this information to interpret the crash.
print("")
print("The Equation has no real roots.")
exitloop = False
while exitloop == False:
selection = input("Would you like to return to the main menu (y/n)? >>> ")
if selection == "n":
exit()
elif selection == "y":
exitloop = True
print("")
print("----------------------------------------------------------")
print("")
else:
print("INVALID OPTION")
except:
print("INVALID INPUT. PLEASE ENTER ONE NUMBER VALUE ONLY")
validinputs = False
elif choice == 2:
print("")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
print(">>> USES OF QUADRATIC EQUATIONS IN A REALISTIC CONTEXT <<<")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
print("")
print("Quadratic equations have many uses in the world. They can be")
print("used to solve many challenging problems. Whether you realise")
print("it or not, you will have used or experienced something that")
print("has been designed and created to be as it is as a direct result")
print("of solving quadratic equations.")
print("For instance, arc of light the headlights on a car make can be")
print("worked out using the quadratic formula, allowing automotive engineers")
print("to work out how bright and how big to make the bulbs in headlights.")
print("----------------------------------------------------------")
print("below are some common uses of quadratic equations:")
print("")
print("- Working out the time of flight of a projectile (for example a bullet)")
print("")
print("- Calculating the perfect trajectory of a drop goal in a rugby match")
print("")
print("- Calculating stopping distances of cars (this is useful for car safety design")
print("")
print("- Used extensively in quantum theory")
print("")
print("- Structural problems for architects and civil engineers")
print("")
print("- Clock designs")
print("")
print("- Economics and financial problems (for example investment models")
print("")
print("----------------------------------------------------------")
exitloop = False
while exitloop == False:
selection = input("Would you like to return to the main menu (y/n)? >>> ")
if selection == "n":
exit()
elif selection == "y":
exitloop = True
print("")
print("----------------------------------------------------------")
print("")
else:
print("INVALID OPTION")
elif choice == 3:
exit()
else:
print("")
print("INVALID INPUT. PLEASE TRY AGAIN")
print("")
if __name__ == "__main__":
#####################################################################
### calls the menu function automatically when the program starts ###
#####################################################################
menu()
```