Using try and except statement for exception handling, using raise to throw out the exception and stop the program.
Use the sort() method to sort the array. Use the len() method to return the length of an array (the number of elements in an array).
An if else statement is a conditional statement that runs a different set of statements depending on whether an expression is true or false.
The def keyword defines a function. A function is a group of statements that can be called upon repeatedly to perform a specific task.
Write a code or draw a flowchart to find the median of a given data set.
The print built-in function displays the specified string to the output.
X
A variable stores a value that can be referred to or modified later in the program. A name, medPos, is assigned to the variable as an identifier in this case.
Game Code
The for loop repeats codes within itself for a fixed number of times determined by the specified expression.
00:00
def load_input(input_str):
try:
inputDatas = input(input_str).split(",")
integerDatas = []
for n in inputDatas:
integerDatas.append(int(n))
return integerDatas
except ValueError:
print("Enter whole numbers split by ',' only!")
raise
dataSet = load_input("Enter numbers split by ',': ")
dataSet.sort()
l = len(dataSet)
medPos = int(l/2)
if l % 2 == 0:
median = (int(dataSet[medPos]) + int(dataSet[medPos-1]))/2
else:
median = dataSet[medPos]
print("The median of the data set is {median}".format(median = median))
Use append() method to an element at the end of the array. In this case is converting the input data into integer and adding them to the array.