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. Only one block among the several blocks of if...elif...else is executed according to the condition.
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. We use this to decide whether the graph cut the x-axis.
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 value of a is not -1 or 1, the system will output the error message and terminate the program.
Write a code or draw a flowchart about the graph of y = ax2 + bx + c. The inputs are the value of a, which is 1 or −1, and the value of b and of c. The output is the coordinates of the turning point of the graph and a statement whether the graph cut the x-axis.
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 calculated and stored previously.
X
In this case, if the y0 == 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
# input a,b,c
a = float(input('Type a (-1 or 1): '))
b = float(input('Type b: '))
c = float(input('Type c: '))
if not a in [-1, 1]:
print("a must be -1 or 1, exit")
quit()
# turning point
x0 = -b/2*a
y0 = a * x0 * x0 + b * x0 + c
print(f'The turning point of this graph is ({x0}, {y0}).')
# cut x-axis
if y0 == 0:
print('The graph touches the x-axis.')
elif a*y0 > 0:
print('The graph doesn\'t cut the x-axis.')
else:
print('The graph cuts the x-axis at two distinct points.')
If the previous condition is false, the program will check elif condition a*y0 > 0, if true, it will execute statement inside elif block. 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, x0 and y0 are the variable to store the values of turning point of this graph.