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. The float() method will converts the number into a number with a decimal point. The split() method splits a string into a list.
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.
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 theta, variable sin_ACB and variable S for future use.
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 give the outputs ∠ACB in radians and the area of a triangle ABC in a coordinate plane, where the inputs are the coordinates of A, B and C.
The print() built-in function displays the specified string to the output. In this case, we use print() function to outp the result.
Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it
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 A = list(map(float,input("Type coordinates of A without the brackets e.g. If the coordinates are \"(1,2)\", type \"1,2\": ").split(","))) B = list(map(float,input("Type coordinates of B without the brackets e.g. If the coordinates are \"(1,2)\", type \"1,2\": ").split(","))) C = list(map(float,input("Type coordinates of C without the brackets e.g. If the coordinates are \"(1,2)\", type \"1,2\": ").split(","))) #Find the lengths of three sides of triangle ABC a =  math.sqrt((B[0]-C[0])*(B[0]-C[0]) + (B[1]-C[1])*(B[1]-C[1])) b =  math.sqrt((A[0]-C[0])*(A[0]-C[0]) + (A[1]-C[1])*(A[1]-C[1])) c =  math.sqrt((A[0]- B[0])*(A[0]-B[0]) + (A[1]-B[1])*(A[1]-B[1])) #Use cosine rule to find the angle ACB theta = math.acos((a*a + b*b - c*c) / ( 2 * a * b )) sin_ACB = math.sin(theta) S = round(0.5 * a * b * sin_ACB, 3) print(f'The size of angle ACB is {round(theta,3)}.The area of the triangle ABC is {S}.')
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 a, b and c for future use.