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 len() function returns the number of items in an object.
The input() method, can be used to ask the user for input. The split() method splits a string into a list.
An array is a special variable, which can hold more than one value at a time, and you can access the values by referring to an index number. The index number start with 0. In this case, we use the user input list as array.
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 n is not equals to 9, the system will output the error message and terminate the program.
Write a code or draw a flowchart to find the interquartile range of a data set with 9 data points. The input is the list of data points in the data set.
The print() built-in function displays the specified string to the output. In this case, we use print() function to output the result.
The append() method appends an element to the end of the list. The pop() method removes the element at the specified position and returns the removed value.
X
The sort() method sorts the list ascending by default.
The import statement is used to get access to code from another module by importing the file/function. In this case, statistics is a built-in module that you can use to calculate mathematical statistics of numeric data.
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 statistics
#input 9 data points with "," as separator
dataset = [int(i) for i in input("Input 9 data points (with \",\" as separator): ").split(",")]
#check number of data is it equal to 9
n = len(dataset)
if n!=9:
print(f'Number of data points is not 9. ')
quit()
#sort dataset in ascending order
dataset.sort()
#calculate Q1(mean of the data in 2nd and 3rd positions) and Q3(mean of the data in 7th and 8th positions)
Q1 = statistics.mean([dataset[1],dataset[2]])
Q3 = statistics.mean([dataset[6],dataset[7]])
print(f'Interquartile range of dataset = {Q3-Q1}.')
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 C and n for future use.
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.