Using try and except statement for exception handling, using raise to throw out the exception and stop the program.
If the previous condition is false, the program will execute statement inside else block to calculate x1 and x2 values. The math.sqrt() function will be used to calculate the square root of D.
The input() method, can be used to ask the user for input. In this case, we required user to input value of a, value of b and value of c. The float() method will converts the number into a number with a decimal point.
The if...elif...else statement is used for decision making. In this case, we use this to decide the final output of the program. Only one block among the several blocks of if...elif...else is executed according to the condition.
Comments starts with a "#" symbol, and Python will ignore them. Comment can be used to explain Python code, and make the code more readable.
When the system encounters the quit() function, it terminates the execution of the program completely. In this case, if the value of a is equals to 0, the system will output the error message and terminate the program.
Write a code or draw a flowchart to solve the quadratic equation ax2 + bx + c = 0, where the inputs are a, b and c. When there are no real roots, the output is, 'The equation ax2 + bx + c = 0 does not have real roots.'
The print() built-in function displays the specified string to the output. In this case, we use print() function to show variables x0 and y0 that we calculate and store previously.
X
The import statement is used to get access to code from another module by importing the file/function. In this case, math is imported to provides access to the mathematical functions such as math.sqrt().
In this case, if the D < 0 condition is true, it will execute statement inside if block, if false, it will check next condition.
Game Code
The for loop repeats codes within itself for a fixed number of times determined by the specified expression.
00:00
import math
#input a,b,c
a = float(input('Type a (non-zero): '))
b = float(input('Type b: '))
c = float(input('Type c: '))
if a == 0:
print(f'a needs to be non-zero.')
quit()
#calculate D
D = b * b - 4 * a * c
#prepare ax^2 +bx +c = 0 equation
equation = f'{a} x^2 + {b} x + {c} = 0'
#make decision of the output
if D < 0:
print(f'The equation {equation} does not have real roots.')
elif D == 0:
x0 = -b/(2*a)
print(f'The equation {equation} has two repeated roots: x = {x0}.')
else:
x1 = (-b - math.sqrt(D))/(2*a)
x2 = (-b + math.sqrt(D))/(2*a)
print(f'The equation {equation} has two distinct real roots: x = {x1} and x = {x2}.’)
If the previous condition is false, the program will check elif condition D == 0, if true, it will execute statement inside elif block to calculate x0 and output the result. If false, it will check next condition.
A variable stores a value that can be referred to or modified later in the program. In this case, the result of calculation will store in variable D and the ax^2+bx+c=0 will store in variable equation for future use.