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

CSC 102 Assignment.

The document describes a Python program to calculate a student's grade based on their assignment, test, and lab scores. It defines functions to calculate the weighted average and assign a letter grade. The program then prompts the user to input scores and prints the final grade. It also includes Python programs to calculate the Fibonacci sequence and BMI.

Uploaded by

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

CSC 102 Assignment.

The document describes a Python program to calculate a student's grade based on their assignment, test, and lab scores. It defines functions to calculate the weighted average and assign a letter grade. The program then prompts the user to input scores and prints the final grade. It also includes Python programs to calculate the Fibonacci sequence and BMI.

Uploaded by

Simon Adediran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Federal University Dutse, Jigawa State.

College of Medicine and Allied Health Sciences.


Faculty of Basic Medical Sciences.
Department of Human Anatomy.
CSC 102: INTRODUCTION TO PROBLEM SOLVING SKILLS.
ASSIGNMENT: 1. Write a Python program to calculate student’s
grade using C.A. and Exams.
Submitted by: BMS/ANT/22/1020 Simon Adediran

Introduction
In everyday activities, a variety of tasks such as data analysis, data
visualization, development of websites and software and organizing
finances are part of our daily lives. But these tasks are often
tedious(bulky) if done manually, hence the use of computers. But in order
to perform all these tasks, we have to communicate with the computer,
since computers do not understand the natural language, there is need
therefore to provide a medium to interact with the computer in order to
perform these tasks. Python as a programming language is one of those
languages with which we can interact with the computer.
Python is easy to learn, therefore it is a suitable language to solve
tasks such as calculate a student’s grade. Therefore, we will proceed to
write a Python program to calculate student’s Grade. Below is the
program code to calculate student’s grade.
# Define the weights for each component
weight_assignment = 0.1 # 10% of the final grade
weight_test = 0.7 # 70% of the final grade
weight_lab = 0.2 # 20% of the final grade

# Define the grading scale


grade_A = 70 # score >= 70 is A

1
grade_B = 60 # score >= 60 and < 70 is B
grade_C = 50 # score >= 50 and < 60 is C
grade_D = 40 # score >= 40 and < 50 is D
grade_E = 35 # score >= 35 and < 40 is E
grade_F = 0 # score < 35 is F

# Define a function to calculate the final grade


def calculate_grade (assignment_score, test_score,
lab_score):
# Calculate the weighted average of the scores
final_score = weight_assignment * assignment_score +
weight_test * test_score + weight_lab * lab_score
# Assign a letter grade based on the grading scale
if final_score >= grade_A:
letter_grade = 'A'
elif final_score >= grade_B:
letter_grade = 'B'
elif final_score >= grade_C:
letter_grade = 'C'
elif final_score >= grade_D:
letter_grade = 'D'
elif final_score >= grade_E:
letter_grade = 'E'
else:
letter_grade = 'F'
# Return the final score and the letter grade
return final_score, letter_grade

2
# Ask the user to enter the scores for each component
assignment_score = float (input ('Enter the assignment
score: '))
test_score = float (input ('Enter the test score: '))
lab_score = float (input ('Enter the lab score: '))

# Call the function to calculate the grade


final_score, letter_grade = calculate_grade
(assignment_score, test_score, lab_score)

# Print the result


print ('Your final score is: ', final_score)
print ('Your letter grade is: ', letter_grade)

Explanation of the Python program.


Here is a step-by-step breakdown of what the program does:

- First, it defines a function called `grade` that takes a score as a parameter and
returns a letter grade based on the score. The function uses a series of `if-elif-
else` statements to check the score against different ranges and assign the
corresponding grade. For example, if the score is between 70 and 100, the
function returns "A".
- Next, it defines another function called `weighted_average` that takes two lists
as parameters: `scores` and `weights`. The function calculates and returns the
weighted average of the scores by multiplying each score by its weight and
adding them up. For example, if the scores are [80, 90] and the weights are [0.4,
0.6], the function returns (80 * 0.4) + (90 * 0.6) = 86.
- Then, it defines two variables called `ca_weight` and `exam_weight` that store
the weights for continuous assessment and exam, respectively. In this case, the
weights are 0.4 and 0.6, meaning that the continuous assessment counts for 40%
of the final score and the exam counts for 60%.

3
- After that, it asks the user to enter the number of students and stores it in a
variable called `n`. It uses the `int` function to convert the user input from a
string to an integer.
- Next, it uses a `for` loop to iterate through each student from 0 to n-1. For
each student, it does the following:
- It asks the user to enter the student's name and stores it in a variable called
`name`.
- It asks the user to enter the continuous assessment score and stores it in a
variable called `ca_score`. It uses the `float` function to convert the user input
from a string to a floating-point number.
- It asks the user to enter the exam score and stores it in a variable called
`exam_score`. It uses the `float` function to convert the user input from a string
to a floating-point number.
- It calls the `weighted_average` function with the arguments `[ca_score,
exam_score]` and `[ca_weight, exam_weight]` and stores the result in a variable
called `final_score`. This calculates the final score for the student as the
weighted average of the continuous assessment and exam scores.
- It calls the `grade` function with the argument `final_score` and stores the
result in a variable called `final_grade`. This calculates the letter grade for the
student based on the final score.
- It prints the final score and grade of the student using the `print` function
and the `format` method. The `format` method replaces the placeholders `{}`
with the values of the variables inside the parentheses. For example, `print("{}
has a final score of {} and a grade of {}.".format(name, final_score,
final_grade))` prints something like "Ibrahim has a final score of 65.0 and a
grade of B.".
In conclusion, from the above program, it is obvious to that a Python
program is easy to write and is very functional(very useful), and this step by
step program is able to calculate a student’s grade using C.A. and Exams.
2. Write a Python program to compute Fibonacci sequence.
This program code is to compute Fibonacci sequence and is written below;
# Define a function to compute the nth Fibonacci
number
Def fibonacci(n):

4
# Check if n is 0 or 1
If n <= 1:
# Return n as the base case
Return n
Else:
# Return the sum of the previous two Fibonacci
numbers recursively
Return fibonacci(n-1) + fibonacci(n-2)

# Ask the user to enter the number of terms


N = int(input(“How many terms? “))

# Check if the number of terms is valid


If n <= 0:
Print(“Please enter a positive integer”)
Else:
# Print the Fibonacci sequence
Print(“Fibonacci sequence:”)
For I in range(n):
Print(fibonacci(i))
Explanation of the Fibonacci sequence program.
Below is a line by line explanation of the Fibonacci sequence program;

- The first line defines a function named `fibonacci` that takes one parameter
`n`, which is the index of the Fibonacci number to compute.

5
- The second line checks if `n` is less than or equal to 1, which means it is either
0 or 1. These are the base cases of the Fibonacci sequence, where the 0th term is
0 and the 1st term is 1.
- The third line returns `n` as the result of the function if it is a base case. This
means that `fibonacci(0)` returns 0 and `fibonacci(1)` returns 1.
- The fourth line is the `else` clause, which means that `n` is greater than 1 and
not a base case. In this case, the function needs to compute the Fibonacci
number using the recurrence relation.
- The fifth line returns the sum of the previous two Fibonacci numbers by
calling the `fibonacci` function recursively with `n-1` and `n-2` as the
arguments. This means that `fibonacci(n)` returns `fibonacci(n-1) + fibonacci(n-
2)`. For example, `fibonacci(3)` returns `fibonacci(2) + fibonacci(1)`, which is 1
+ 1 = 2.
- The seventh line asks the user to enter the number of terms to print and stores
it in a variable called `n`. It uses the `int` function to convert the user input from
a string to an integer.
- The ninth line checks if the number of terms is valid, meaning that it is a
positive integer. If it is not, then the program prints an error message and exits.
- The eleventh line is the `else` clause, which means that the number of terms is
valid and the program can proceed to print the Fibonacci sequence.
- The twelfth line prints a header that says “Fibonacci sequence:” to indicate the
start of the output.
- The thirteenth line uses a `for` loop to iterate through each term from 0 to n-1.
For each term, it does the following:
- The fourteenth line calls the `fibonacci` function with the current term as the
argument and prints the result. This means that it prints the Fibonacci number
corresponding to the term. For example, if the term is 3, it prints `fibonacci(3)`,
which is 2.
3. Write a Python program to calculate BMI using weight/height.
Sure, I can help you with that. Here is a possible Python program that can
calculate the BMI for weight and height:
```python
# Define a function to calculate the BMI from weight and
height

6
Def bmi(weight, height):
# Convert weight from kg to pounds
Weight = weight * 2.205
# Convert height from cm to inches
Height = height * 0.394
# Calculate the BMI using the formula
Bmi = (weight / (height ** 2)) * 703
# Return the BMI value
Return bmi

# Ask the user to enter their weight in kg


Weight = float(input(“Enter your weight in kg: “))
# Ask the user to enter their height in cm
Height = float(input(“Enter your height in cm: “))
# Call the bmi function with weight and height as
arguments
Bmi_value = bmi(weight, height)
# Print the BMI value
Print(f”Your BMI is {bmi_value:.2f}”)
```

This program uses a function called `BMI`` that takes two parameters: `weight`
and `height`. The function converts the weight from kg to pounds and the height
from cm to inches, and then calculates the BMI using the formula¹:

BMI = {weight (lb)}/{height^2 (in)} \times 703

7
The program asks the user to enter their weight in kg and their height in cm, and
then calls the `bmi` function with those values as arguments. The function
returns the BMI value, which the program prints with two decimal place.
REFERENCES.
-Wong, M. (n.d.). A Python guide to the Fibonacci sequence. [PROGRAMIZ].
Retrieved November 9, 2023, from https://www.programiz.com/python-
programming/examples/fibonacci-sequence
-CODESCRACKER. (n.d.). Python program to calculate student’s grade.
Retrieved November 4, 2023, from
https://codescracker.com/python/program/python-program-calculate-student-
grade.htm
-Bansal, I. (September 30th, 2021.). BMI calculator in Python – A Complete
Step-By-Step Tutorial. [ASKPYTHON]. Retrieved November 9, 2023, from
https://www.askpython.com/python/examples/bmi-calculator

You might also like