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

Activity 4

The document outlines an activity on writing user-defined functions in Python. It provides examples of built-in functions and asks the student to write their own function to calculate the general weighted average of grades input by the user, with options to add grades or calculate the average. The student's code is documented with comments.

Uploaded by

Renz Abrigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Activity 4

The document outlines an activity on writing user-defined functions in Python. It provides examples of built-in functions and asks the student to write their own function to calculate the general weighted average of grades input by the user, with options to add grades or calculate the average. The student's code is documented with comments.

Uploaded by

Renz Abrigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Republic of the Philippines

BATANGAS STATE UNIVERSITY


The National Engineering University
Alangilan Campus
Golden Country Homes, Alangilan Batangas City, Batangas, Philippines 4200
Tel Nos.: (+63 43) 425-0139 local 2121 / 2221
E-mail Address: [email protected] | Website Address: http://www.batstate-u.edu.ph

College of Engineering - Department of Electrical Engineering

Bachelor of Science in Computer Engineering

CpE 401: COMPUTER PROGRAMMING 1


2nd Semester A.Y. 2023 – 2024

Activity 4
FUNCTIONS

SUBMITTED BY:
Abrigo, Renz Christian, B.

COE - 1203

SUBMITTED TO:
Sir Mark Rondol P. Abdon

Leading Innovations, Transforming Lives, Building the Nation


ACTIVITY 4: FUNCTIONS

Direction: Write 10 examples of one line code that has built in functions. (1 point each)
Example: char_count = len("hello")

1. hypotenuse = ((((3)**2)+((4)**2))**0.5)

2. total = sum([5, 10, 15, 20, 25, 30])

3. absolute_value = abs(-10)

4. all_caps = ("title").upper()

5. title_case = ("welcome to my world").title()

6. lower_case = (“HELLO THERE”).lower()

7. rounded_value = round(3.14159265359, 2)

8. change = ("Genshin Impact").replace("Genshin", "Honkai")

9. triple = ("well…") * 3

10. whole_number = int(67.4992)

Direction: Write your own user-defined function. (20 points)

Please provide a screenshot of your program. Remember, it's essential to maintain originality when completing this
task.
Republic of the Philippines
BATANGAS STATE UNIVERSITY
The National Engineering University
Alangilan Campus
Golden Country Homes, Alangilan Batangas City, Batangas, Philippines 4200
Tel Nos.: (+63 43) 425-0139 local 2121 / 2221
E-mail Address: [email protected] | Website Address: http://www.batstate-u.edu.ph

College of Engineering - Department of Electrical Engineering

Paste your code here use comments to properly document your program.
grades = [] #List of values of grades

def add_grade(): #Add the value of your grade


grade_value = float(input("Enter your grade: "))
if grade_value <= 0:
print("Error. Please input a value greater than zero.")
else: #The values you enter will be listed
grades.append(grade_value)
print("Added your grade.")

def calc_total(): #Calculated the General Weighted Average


if grades == []:
print("There are no grades added.")
else:
average = sum(grades)/len(grades)
print("Your General Weighted Average is: ", average)

def main(): #A loop that lets you choose if you want to add a grade, calculate the grades you listed, or exit the
program
while True:
print("Calculate your General Weighted Average!")
print("1. Add a Grade")
print("2. Calculate Average Grade")
print("3. Exit")
choice = int(input("Enter option: "))
if choice == 1: #Choose to add grade
print("\n")
add_grade()
elif choice == 2: #Choose to calculated the average of your grade
print("\n")
calc_total()
elif choice == 3: #Choose to exit the program
print("\n")
print("Now exiting program...")
break
else: #The user shall only pick from options 1-3
print("Please pick from options 1-3 only.")

main()

Leading Innovations, Transforming Lives, Building the Nation


Snapshot of the python code

Snapshot of the output


Calculating the General Weighted Average:

If value is less than 0: Exiting the Program:

You might also like