0% found this document useful (0 votes)
14 views

Computation Program With Report

Uploaded by

Vadim Tudor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Computation Program With Report

Uploaded by

Vadim Tudor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Computation Program with Report

Ghinda Dionis
23024853
CS4051 Fundamentals of computing
Spring 2023-24
Introduction - 2

Description - 2

Testing - 3

Reflections - 4

Code + comments - 5

1
Introduction
This report is the final product of the “Introduction to Comptuing”
course. The main focus of the course was learning and introducing us
students to Python, a programming language that’s powerful and
somewhat beginner-friendly.
My final task was to build a Python program that is capable of taking
marks as user input and as a result transform this information into
statistical data. The main analitical functions where:
 mode - the number that occurs the most times in a group of
numbers.
 median - the middle value in a group of values; that is, half the
values are greater than the miedian number and half the values are
less than the median number.
 mean - set of varialbes is their sum deivded by their number.
 skewness - the measure that indicates the asymmetry of the data. A
positive skew means the presence of more high marks, while a
negaative skew means the frequenct of several small marks.

Description
get_student_marks is used to take a list of marks of the students as
input, the program ensures that the user has introduced at least 2 marks

find_mode is used to find the mode from the mark list. It will calculate
which number is repeated the most and then it will return that number.
If all the numbers have the same frequency it returns the result

find_median function receives a list of numbers and finds the median.


First, it arranges the list in an ascending order, which is important for
finding the middle number and the median. The function finds the
median value depending on whether the numbers in the list are even or
odd it finds the average of the two middle numbers for an even count,
while it picks the middle number directly for an odd count.

calculate_mean is meant to find the average or mean of a list of


numbers. It finds the mean by dividing the sum of the numbers by the
total number of elements in the list. It returns the calculated mean

2
together with a formatted message showing the mean to two decimal
points.

calculate_standard_deviation function will calculate the standard


deviation for a given list of numbers. Basically, it is a measure of the
amount of variation or dispersion there is in a set of numerical data. The
function calculates the mean of the numbers first,the it works out the
standard deviation by taking the square root of the variance. The
function does return a measure of how spread out the numbers are
from their average value.

calculate_skewness takes a list of numbers as input and calculates the


skewness of those numbers. Skewness refers to the measure of data
distribution asymmetry around the mean. If the list is empty, the
function will return the message that says an error was done that there
are no numbers entered. The function calls upon If the standard
deviation equals zero, it will return an error message, 'standard
deviation is 0, skewness is undifined'. Skewness of the data is then
determined by using the formula 3 *(mean - median) / std_dev , in such
a way that it explains to us how much the distribution favors one side of
the mean. The result is then returned.

main function is the main controlling point of the application that


calculates the statistics from a list of marks. It has a menu that presents
a list of actions and then takes action based on the user's response.

display_menu function is to serve as a user interface. The menu of


choices will be displayed regarding the different possibilities for
manipulating the data.
Testing
Entering values ranging from 1-10

Calculating mean.

3
Calculating median.

Calculating mode.

Calculating standard deviation.

Calculating skewness.

Adding new marks to the array.

Adding a number, whilist trying to introduce an invalid value to test the


program.

Recalculating the mean to see if the mark list updated.

Adding a new list to the program.

testing the menu.

Reflections

This Python program was a great exercise for learning and practicing
different concepts that I have discovered this semester in the
“Fundamentals of Computing” module. I have understood how to
implement basic algorithms, I had to figure out ways to implement the
mode, mean, median, and skewness, using mathematics, logic, and the

4
Python programming language. Because this program needed so
many different functions I have learned to break tasks into smaller, more
manageable ones and organize them better. It was my first time creating
a project this big in Python so I had to research different methods, such
as dictionaries, lists, and definitions. Overall it was a great
experience and I feel I have learned a lot during this process.

The code + comments


def get_student_marks(marks=None): # definition of the get student marks function

if marks is None: #checks if marks is not provided

marks = [] #initilizes marks as an empty list

print("Enter the marks of students one at a time, or multiple marks separated by commas.
Type 'done' to finish.") #print instruction for the user on how to input marks

while True: # start an infinite loop which will run until it is stopped

user_input = input("Enter mark(s) or 'done': ") #this user input line causes the user to
enter a mark or write done

if user_input.lower() == 'done': #converts the input to lowercase to enter the marks or


'done' to stop entering marks

if len(marks) < 2: #check the number of marks to be bigger than 2

print("Please enter at least two marks.") #if the user introduces less than 2 marks it
informs the user to add at least 2

continue #continues the loop until the required amount of marks has been
introduced

break #it breaks the loop if more than 2 marks have been introduced

if ',' in user_input: #if there are commas in the input the program divides the String by
commas

try: #test the code for errors

marks.extend(float(mark.strip()) for mark in user_input.split(',')) #convert each


number to float and add it to the marks list

except ValueError:#handle the error

5
print("Invalid input detected. Please enter only numeric values separated by
commas.") #if it's not a valid number it will print an error message

else: #if there is no error continue executing the code

try: #start the following block of code to test it, it is used here because converting a
String to a float might not work if the string isn't a valid number

mark = float(user_input) #convert the text input into a float

marks.append(mark) #if the conversion is successful the number is then added to


the list called 'marks' using append

except ValueError: #catches errors that could happen with try; it looks for the
ValueError which is an error when Python tries to convert a non-number into a number

print("Invalid input. Please enter a numeric value.") #this line will print an error
message if the user did not input a numeric value

return marks # end of the get_student_marks

def find_mode(numbers): #define the find_mode function, it takes numbers as input

numbers.sort()#sort the list of numbers in ascending order

frequency = {}#this is a dictionary called 'frequency' that takes into account how many
times each number appears in the list

for num in numbers:#this loop looks through each number in the list

frequency[num] = frequency.get(num, 0) + 1#for each number it changes the dictionary


by that number + 1, if num is not a key in the dictionary it returns 0

max_frequency = max(frequency.values()) #this line finds the maximum value in the


dictionary

modes = [num for num, count in frequency.items() if count == max_frequency] #this list
goes through the dictionary and collects all 'num' whose 'count' is equal to 'max_frequency'

if all(count == 1 for count in frequency.values()): #check if all numbers occur at once

return None, "No mode: All numbers occur the same amount of times." #if true it
returns None

elif len(modes) > 1: #check if there is more than 1 mode

return modes, f"More than one mode: {modes}" #if true returns list of modes

6
return modes[0], f"Mode: {modes[0]}" #if none of the above is true, return only 1 mode

def find_median(numbers): # this list defines find_median function

numbers.sort() # this method organizes the numbers in ascending order, we have to do


this because the median is the number in the middle

n = len(numbers) # this method calculates the total amount of numbers in the list

mid = n // 2 # use floor division to round the number when calculating the mid

if n % 2 == 0: #check if n list is even

median = (numbers[mid - 1] + numbers[mid]) / 2 #adding average of the 2 middle


numbers then divided by 2

else: # if n is odd

median = numbers[mid] # median is the number in the middle

return median, f"Median: {median}" #return the median value

def calculate_mean(numbers): #define caluclate_mean function with input numbers

mean = sum(numbers) / len(numbers) #mean is equal to the sum of the numbers divided
by the amount of elements in the list

return mean, f"Mean: {mean:.2f}" #return the mean value

def calculate_standard_deviation(numbers): # define calculate_standard_deviation function


with numbers

mean = sum(numbers) / len(numbers) #mean is equal to the sum of the numbers divided
by the amount of elements in the list

sum_of_squares = sum((x - mean) ** 2 for x in numbers) #calculate the sum of squares of


the differences between each number and the mean

variance = sum_of_squares / len(numbers) #calculate variance by dividing sum_of_square


to the number of numbers in list

standard_deviation = variance ** 0.5 # Using exponentiation to calculate square root

return standard_deviation #return the standard deviation value

7
def calculate_skewness(numbers): #define calculate_skewness function with numbers

if not numbers: #check if the number list is empty

return "No numbers were entered."#if empty it returns an error message

mean = calculate_mean(numbers)[0] #calls the previous function calculate mean

median, _ = find_median(numbers) #calls the previous function calculate median

std_dev = calculate_standard_deviation(numbers) #calls the previous function standard


deviation

if std_dev == 0: #check if the standard deviation is 0

return "Standard deviation is zero, skewness is undefined." #if its 0 it returns an error
message

skewness = 3 * (mean - median) / std_dev #calculate the skewness by multiplying the


difference of mean and median with 3 and dividing by the standard deviation

return f"Skewness: {skewness:.3f}" #return the value of skewness

def display_menu(): #define the function dislplay_menu

print("\nMenu:") #print menu on the screen

print("1. Print the mean of the numbers") #print and choose the mean of the numbers
action

print("2. Print the median of the numbers") #print and choose the median of the numbers
action

print("3. Print the mode of the numbers") #print and choose the mode of the numbers
action

print("4. Print the standard deviation of the numbers") #print and choose the standard
deviation of the numbers action

print("5. Print the skewness of the numbers") #print and choose the skewness of the
numbers action

print("6. Enter a NEW set of numbers") #print and choose to enter a new set of numbers
action

8
print("7. Add MORE numbers") #print and choose to add more numbers action

print("8. Exit the application") #print and choose exit the application action

choice = input("Enter your choice: ") #allow user to make his choice based on the actions
above

return choice #end function by returning the choice of the user

def main(): #define main function

marks = [] #create an empty list for the marks

while True: #start loop

choice = display_menu() #call the display menu

if choice == '1': #if choice is 1

_, message = calculate_mean(marks) #program calculates the mean of the marks

print(message) #it prints the mean

elif choice == '2': #if choice is 2

_, message = find_median(marks) #program calculates the median of the marks

print(message) #ot prints the median

elif choice == '3': #if choice is 3

_, message = find_mode(marks) #program calculates the mode of the marks

print(message) #it prints the mode

elif choice == '4': #if choice is 4

std_dev = calculate_standard_deviation(marks) #program calculates the standard


deviation of the marks

print(f"Standard Deviation: {std_dev:.3f}") #it prints the standard deviation

elif choice == '5': #if choice is 5

skewness_message = calculate_skewness(marks) #program calculates the skewness


of the marks

print(skewness_message) #it prints the skewness

elif choice == '6': #if choice is 6

9
marks = get_student_marks() # Start with a new set of marks

elif choice == '7': #if choice is 7

marks = get_student_marks(marks) # Append to the existing marks

elif choice == '8': #if choice is 8

print("Exiting the application.") #print a message that says exiting the application

return #end the loop and exit the main function

else: #if none of 1-8 choices are selected

print("Invalid choice. Please enter a number from 1 to 9.") #the program informs the
user that they made an invalid choice

if __name__ == "__main__": #check if __name__ equals __main__ which means that the
script is executed as the main program

main() #call main function

10

You might also like