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 r≤0 or R≤0, the system will output the error message and terminate the program.
Let G(a, b) and H(c, d) be the centres of two circles on a Cartesian plane and r and R be the radii of the circles. Write a code or draw a flow chart to decide if thetwo circles intersect with each other. The inputs are the coordinates of the centres and the radii of the circles. When the circles intersect at points P and Q, the output is the length of the line segment PQ. Whenthe circles do not intersect, the output is 'The two circles do not intersect. '
The print() built-in function displays the specified string to the output. In this case, we use print() function to output the user input.
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
#input G(a,b), H(c,d), r, R
a, b = map(float,input("Type center G of a circle without the brackets e.g. If the coordinates are \"(1,2)\", type \"1,2\": ").split(','))
c, d = map(float,input("Type center H of the other circle without the brackets e.g. If the coordinates are \"(1,2)\", type \"1,2\": ").split(','))
r = float(input('Type r ( r>0 ): '))
R = float(input('Type R ( R>0 ): '))
if r <= 0 or R <= 0:
print(f'r and R both need to be positive.')
quit()
print(f'circle G: centre ({a},{b}) with radius {r}')
print(f'circle H: centre ({c},{d}) with radius {R}')
#find the distance between the centres of the circles
import math
d_centers = math.sqrt( (a-c)*(a-c) + (b-d)*(b-d) )
#Use triangle inequality to test whether the two circles intersect with each other.
sorted_3_lengths = sorted([ d_centers, r, R ])
if sorted_3_lengths[0] + sorted_3_lengths[1] < sorted_3_lengths[2]:
print('The 2 circles don\'t intersect with each other.')
elif sorted_3_lengths[0] + sorted_3_lengths[1] == sorted_3_lengths[2]:
print('The 2 circles touch each other.')
else:
#find the length of PQ using cosine rule
PGH = math.acos((r*r + d_centers*d_centers - R*R)/(2*r*d_centers))
sin_PGH = math.sin(PGH)
PQ = round(2 * r * sin_PGH, 3)
print(f'The 2 circles intersect with each other at P and Q. The length of line segment PQ is {PQ}.')
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.