You are on page 1of 19

ASSIGNMENT 1 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Computing Fundamental

Submission date Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Nguyen Hai Duong Student ID BH01628

Class COM109.07 Assessor name Dinh Van Dong

Student declaration

I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.

Student’s signature Duong

Grading grid

P1 P2 P3 M1 M2 M3 D1 D2 D3
 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:


Lecturer Signature:
Contents
P1: Provide a definition of the data type that you use for the variables. ..................................................................... 5
1.1. Identify the variables used in the program. .................................................................................................. 5
1.2. Specify the data type for each variable. ........................................................................................................ 7
1.3. Briefly explain the reason for choosing the selected data type. ................................................................... 9
P2: Explain the error handling process........................................................................................................................ 11
2.1. Describe the types of errors that the program might encounter. ................................................................... 11
2.2. Outline the steps involved in handling errors using a try-except block. .......................................................... 12
P3: Determine the steps taken from writing code to execution. ................................................................................ 13
3.1. List the steps involved in writing and executing the program. ........................................................................ 13
3.2. Briefly explain the purpose of each step. ......................................................................................................... 14
3.3. Identify the tools and resources used for writing and executing the program. .............................................. 16
Introduction:
In the realm of academia, students often find themselves grappling with the task of calculating
their Grade Point Average (GPA) – a crucial indicator of academic performance. As a diligent student
seeking to streamline this process, I embarked on a journey to develop a Python program capable of
efficiently computing GPA based on grades earned in various courses. Recognizing the significance of
GPA in evaluating academic progress, I embarked on developing a Python program to automate the GPA
calculation process. This program aims to streamline the cumbersome task of manually computing GPA
by providing a convenient and efficient solution for students. In this essay, I will shed light on how to
design and implement a Python program to calculate GPA. The program uses a function-based approach,
allowing users to enter grades and corresponding credits and receive grade point averages seamlessly.
Additionally, I will delve into the rationale behind the program's design choices, including the use of
functions, data structures, and mathematical operations. Through the exploration of this Python program,
readers will gain insights into the intersection of technology and education, witnessing firsthand how
programming can revolutionize traditional academic practices. Join me as we navigate the realm of GPA
calculation and unlock new possibilities for academic excellence through the lens of Python programming.

P1: Provide a definition of the data type that you use for the
variables.

1.1. Identify the variables used in the program.

The variable is part of the program used to store data. Once the variable has been stored, that is, a
space has been allocated in that memory. Based on the type of data of a variable, explaining the creation
of memory and deciding what can be stored in that reserved memory area. Therefore, by assigning
different data to variables, you can store integers, texts, decimals for variables.

The variable is used to store and refer to data values. The variable can be considered as "boxes"
containing information, and they can change the value during the implementation of the program.
Principle of naming variables:

• The name of the variable must be started with a word or an underscore character (lower
bricks: _).
• The name of the variable cannot start with a number. In addition to the output character, in
the variable name can use the number, letters and lower bricks as usual.
• The variable in Python must have a proper name, not duplicating with the names of the
variables existing on your working file.
• The variable name of the flora and normal letters.

Examples of the use of variables:

Referencing websites:
https://stringee.com/vi/blog/post/kieu-du-lieu-trong-python
1.2. Specify the data type for each variable.
Integers include positive integers (1, 2, 3, ..), negative integers (-1, -2, -3) and 0. In Python, the
integer data type is no different special.

Example of integers: Assign a value to a variable a of 4 and output the data type of a.

Regarding the real number data type, this is a set of integers and decimal numbers 1, 1.4, -123,
69.96,...

Example of real number: Assign the value of variable f to 1.23 and output the data type of f.

A series of characters or string is a collection of characters (letters, numbers, special characters


like!, @,#...) in combination. The characters combine together and are in a pair of single clicks `` `` dual
blinks “ ” called a string of characters.

Example:
Bool (Boolean) type in Python is the type of data in which only two values True and False. True
and False are two keywords in Python.

The collection data type is represented by Dict data structure, abbreviated for the word
"Dictionary". A set of data is a data structure that you can store the non-ordered values. Each key must be
unique in the set and is used to access the corresponding value.

Referencing websites:
https://howkteam.vn/course/lap-trinh-python-co-ban/kieu-du-lieu-so-trong-python-1540
https://tuhocict.com/kieu-du-lieu-bool-trong-python/
1.3. Briefly explain the reason for choosing the selected data type.
There are many reasons to choose an integer variable, including:

o Accuracy: variable integer type of integer values accurate, not rounded or lost accuracy due
to rounding errors. This is very important for applications that need high accuracy, such as
financial or scientific applications.
o Performance: The integer variable is usually processed faster than other data types, such as
Float variable. This is due to simpler operations and requires less memory resources.
o Memory size: integer variable is usually smaller than other data types, such as Float
variable (float). This can save memory, especially when storing large amounts of data.
o Compatibility: The integer variable is supported by most programming languages and can
often be used with other data types easily.

For example: To store the number of students in a classroom, you should use an integer variable
because the number of students is an accurate integer.

There are many reasons to choose a real number variable, including:

o Range: Float variables can store a wider range of values than integer variables. This is
important for applications that need to store very large or very small values.
o Precision: Float variables can store numeric values with high precision, although not as
precise as integer variables. This is important for applications that need to perform complex
calculations or process scientific data.
o Flexibility: Float variables can be used to store both integer and float values. This can help
simplify the code and reduce the number of variables needed.
For example: To store the average of test scores in a class, you should use a float variable because
the average can be a decimal number.

There are many reasons to choose string variables, including:

o Storing text: String variables are used to store text, such as names, addresses, or messages.
o Text processing: String variables can be used to perform mathematical operations with text,
such as string concatenation and search and replace.
o Communication: String variables are used to communicate data with users or other
programs.

For example: To store a student's name, you should use a string variable.

Referencing websites:
https://gemini.google.com/app/42f48b73915abab2
P2: Explain the error handling process.

2.1. Describe the types of errors that the program might encounter.
There are two main types of errors that need to be handled in Python programs:

Syntax Error:

• Occurs when the grammar rules of the Python language are violated.
• For example: misspelled keywords, missing brackets, wrong indentation.
• Python immediately reported an error, unable to continue executing the program.

Exception:

• Occurs during program execution due to unexpected conditions.


• For example: division by zero, accessing non-existent file, data entry error.
• Python can continue executing the program after catching the error.
2.2. Outline the steps involved in handling errors using a try-except block.
Step by step error handling using try-except in Python:

• Identify potential errors:


Analyze code to identify sections that may lead to errors.
Consider cases of incorrect input, invalid data access, incorrect mathematical
operations, etc.

• Write a try...except block:


Use try to cover code that may generate errors.
Use except to define specific error handling blocks.

• Determine error type:


Use specific Exceptions to catch errors accurately.
You can use except Exception to catch all Exceptions.

• Error handling:
In the except block, write code to handle the caught error.

• Use else (optional):


The else block is executed if no error occurred in the try block.
Use to execute normal code after successfully processing the data.

• Use finally (optional):


The finally block is always executed regardless of whether an error occurs or not.
Use to perform resource cleanup actions, regardless of outcome.
P3: Determine the steps taken from writing code to execution.

3.1. List the steps involved in writing and executing the program.

The process of step by step handling the problem and explaining each step of the program:

• Create function calculate_gpa():


o This function will calculate GPA based on the list of component scores and the
corresponding number of credits.
o Input: A list containing sets of grades and credits (e.g. [(Points1, Credits1), (Points2,
Credits2), ...]).
o Output: GPA score.
• Use lists to store component scores and credits:
o We will use a list to store sets of points and credits.
• Create print_gpa() function:
o Write the print_gap function to print data values
o This function will print the calculated GPA score.
• Import scores and credits from users:
o The program will prompt the user to enter the corresponding score and number of credits
for each course.
• Handling error cases and invalid data:
o Use Python try-except constructs to handle errors and invalid data.
o For example: If the user enters an invalid name, the program will print an error message.
• Print the results to a text file:
o After completing the calculation, the program will print the results to a text file for storage.
3.2. Briefly explain the purpose of each step.
Explain the implementation goals of each specific step in the program:

• Declaration and Definition of function calculate_gpa(grades):


o Goal: Create a function to calculate GPA based on scores and credits for each course
entered.
o Perform:
▪ grade_points: A dictionary that stores grades corresponding to GPA values.
▪ total_credits and total_grade_points: Variables to accumulate total credits and total
GPA calculated by credit.
• Processing Loop in Function:
o Goal: Browse through each entry in the grades dictionary to calculate total credits and total
GPA.
o Perform:
▪ Checks if grade is in the grade_points dictionary.
▪ Update total_credits and total_grade_points by multiplying the corresponding score
by the number of credits and adding it to the total.
• Calculate and Return GPA:
o Objective: Calculate final GPA by dividing total GPA by total credits.
o Perform:
▪ Check if total credits (total_credits) is zero to avoid division by zero error.
▪ Calculate GPA by dividing total grades (total_grade_points) by total credits
(total_credits).
▪ Round GPA to two decimal places for ease of understanding and clear expression.

o Example of code to calculate real GPA:


• Use the Function and Print Results to the Screen:
o Goal: Test the calculate_gpa function by entering a set of scores and displaying the results.
o Perform:
▪ Call the calculate_gpa function with a score dictionary.
▪ Print out the calculated GPA score to the screen.
o Example of code used to enter data:


o Example of a code snippet that displays data on the screen:

• Record Results to File:


o Goal: Save GPA results to a text file.
o Perform:
▪ Open file: Use the open() function with the file path and "w" (write) mode to
open the gpa_results.txt file. If the file already exists, its old content will be
deleted before writing new data.
▪ Context Manager (with): Use the with statement to manage the context of the
file, ensuring that the file will be closed properly after the with block is
executed, even if an error occurs.
▪ Write data: Use the write() method of the file object to write the GPA string:
{gpa_score} to the file. In this string, {gpa_score} will be replaced with the
calculated GPA value, making this a record of the GPA score.
o Example code to handle writing to file:
3.3. Identify the tools and resources used for writing and executing the
program.
Tools and resources used:

o Python programming language: The program is written in Python programming


language, a flexible and easy-to-read programming language.
o Python runtime environment: To execute a Python program, a suitable Python runtime
environment is used, such as Python Interpreter.
o Source code editor: Any source code editor can be used to write programs, such as
Visual Studio Code, PyCharm, or online source code editors such as Replit or Jupyter
Notebook.
o Source file: The program is stored in a source file in .py format.
o Computer or execution environment: The program can be executed on a personal
computer or an online Python runtime environment.
o File resource: In this example, a text file named "gpa_results.txt" is used to store the
results of GPA calculation.
o Environment variables: Make sure Python Interpreter is installed and the path to it has
been added to the PATH environment variable so that the Python program can be
executed from any directory on the system.

Program implementation:

o Defining the calculate_gpa function: First, the program defines a function called
calculate_gpa, which is used to calculate GPA based on the scores and credits of the
entered subjects.
o Data processing: The program goes through each item in the grades dictionary, checks
whether the grades are valid or not, calculates the total number of credits and total GPA
based on the grade and number of credits for each subject.
o Calculate and export results: Then, the program calculates the final GPA by dividing
the total GPA by the total number of credits and outputs the results to the console
screen.
o Writing results to a text file: GPA results are written to the text file "gpa_results.txt" by
opening the file with write mode and writing the results as the string "GPA:
{gpa_score}".
Tools and resources used to write and execute programs:

• Python Interpreter:
o You need to have Python installed on your computer. Make sure you are using Python
version 3.x as it supports features like f-string which you used in the program to write the
file.
• Integrated Development Environment (IDE) or Text Editor:
o IDEs like PyCharm, Visual Studio Code, or Jupyter Notebook: These tools support Python
development by providing features like debugging, code completion, and environment
management.
• Command Line Interface (CLI):
o Terminal (macOS or Linux) or Command Prompt (Windows): To run Python commands
from the command line and execute scripts.
• Basic knowledge of Python:
o Understanding of control structures (loops, conditional statements), how to work with
dictionaries, and how to handle files.
• File system access rights:
o Make sure you have permission to create and write to files in the directory you are working
in.

Introduction to Pycharm / Visual Studio Code:

• Visual Studio Code is a light but powerful source editing process, running on your desktop
and available for Windows, MacOS and Linux. It comes with integrated support for
JavaScript, TypeScript and Node.js, and has a rich expansion ecosystem for other
languages and running time (such as C ++, C#, Java, Python , PHP, GO, .NET).

• Pycharm is an IDE (Integrated Development Environment) developed by Jetbrains,


specially designed for software development in Python programming language. Considered
one of the leading Python development tools, Pycharm offers a range of features and tools
to help Python developers increase performance and save time during development.
Instructions for using PyCharm:

• Create new project:


o Open PyCharm and select "Create New Project".
o Select the project storage folder and set the project settings.
• Create new Python file:
o In the project window, right-click and select "New" -> "Python File".
o Name the file and start writing Python code.
• Run the program:
o When you have finished writing the program, press Shift + F10 or select "Run"
-> "Run 'File name'".

Introductions for using Visual Studio Code:

• Create new project:


o Open VS Code and create a new folder for your project.
o Open that project folder in VS Code by selecting "File" -> "Open Folder".
• Create new Python file:
o In VS Code, select "File" -> "New File".
o Name the file with the .py extension and start writing Python code.
• Run the program:
o Open terminal in VS Code by selecting "View" -> "Terminal".
o Type the python command name_file.py and press Enter to run the program.
Conclusion:
During the Assignment process, we built a Python program to calculate GPA based on points and
credits of courses. This program not only calculates GPA but also has the ability to handle errors and print
the results to a text file. This helps students easily track their academic performance and make reasonable
decisions about their studies. Things learned: Functional programming: Implement GPA calculation by
building a function in Python, which increases flexibility and code reuse. Error handling and input
checking: Learn how to handle errors and invalid data, helping the program operate smoothly and reliably.
Storing and printing results: Store calculation results in a text file, helping students easily access and refer
back to their learning results. Unmet Constraints: Check for valid names: No checks have been performed
to ensure that the names of the courses entered are valid. This can lead to incorrect names being entered
and cause unexpected errors in the program. Expanded features: The program only focuses on calculating
GPA from a provided list of scores and credits. One improvement could be to allow users to import
information from different sources, such as text files or spreadsheets, to increase flexibility and
convenience for users. Although Assignment's primary goals have been achieved, there remains room for
improvement and expansion of the program in the future.

…………………………End…………………………

Thank you to those who read and rated this essay. Thanks!!

References:
Referencing Wedsite: https://chat.openai.com/

Referencing Wedsite: https://stringee.com/vi/blog/post/kieu-du-lieu-trong-python


Referencing Wedsite: https://howkteam.vn/course/lap-trinh-python-co-ban/kieu-du-lieu-so-trong-
python-1540
Referencing Wedsite: https://tuhocict.com/kieu-du-lieu-bool-trong-python/

Referencing Wedsite: https://gemini.google.com/app/42f48b73915abab2

You might also like