0% found this document useful (0 votes)
39 views6 pages

Lab Manual - AETN2302 - L8 (Function-I)

This document provides instructions for a lab on functions. It has 3 parts - converting between fuel efficiency units, checking if a year is a leap year, and getting the number of days in a month. The document provides sample code and test cases for students to complete the functions.

Uploaded by

Zille Huma
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)
39 views6 pages

Lab Manual - AETN2302 - L8 (Function-I)

This document provides instructions for a lab on functions. It has 3 parts - converting between fuel efficiency units, checking if a year is a leap year, and getting the number of days in a month. The document provides sample code and test cases for students to complete the functions.

Uploaded by

Zille Huma
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/ 6

End of

Lab 8 – “Functions I”
Student Name Date: 09/11/2023

Lab Instructions and Rules

1. If you arrive later than 10 minutes after the lab started, you will not be allowed to
do the lab.
3. There is no time allocated to redo any missed labs.
4. Absence from labs will result in a grade of zero unless a written medical excuse is
provided.

OBJECTIVES
1. Start programming based on functions;
2. Utilizing the return statement

EQUIPMENT
1. Pcs
2. Sandbox

PART I

Scenario: A car's fuel consumption may be expressed in many different ways. For example, in
Europe, it is shown as the amount of fuel consumed per 100 kilometers. In the USA, it is shown
as the number of miles traveled by a car using one gallon of fuel. Your task is to write a pair of
functions converting l/100km into mpg, and vice versa.

The functions are named liters_100km_to_miles_gallon and miles_gallon_to_liters_100km


respectively; take one argument (the value corresponding to their names). Complete the code in
the editor.

def liters_100km_to_miles_gallon(liters):
gallon = liters/3.78541178
miles = 100/1.609344
return miles/gallon

def miles_gallon_to_liters_100km(miles):
km = miles/1.609344
liters = 3.785411784
liters_km = liters*100/km
return liters_km

print(liters_100km_to_miles_gallon(3.9))
print(liters_100km_to_miles_gallon(7.5))
print(liters_100km_to_miles_gallon(10.))
print(miles_gallon_to_liters_100km(60.3))
print(miles_gallon_to_liters_100km(31.4))
print(miles_gallon_to_liters_100km(23.5))

Expected output
60.31143162393162
31.36194444444444
23.52145833333333
3.9007393587617467
7.490910297239916
10.00913120567375

PART II

Scenario: Your task is to write and test a function that takes one argument (a year) and returns
True if the year is a leap year, or False otherwise.

The seed of the function is already shown in the skeleton code in the editor. You can use what
you did in Lab 4 here to complete the function. Complete it and test it for different years by
calling it.

Your code

def is_year_leap(year):
if year%4==0 and year%100!=0:
leap=True
elif year%400==0:
leap=True
else:
leap=False
return leap
print(is_year_leap(2000))
print(is_year_leap(2001))
We have also prepared a short testing code, which you can use to test your function. The code
uses two lists ‒ one with the test data, and the other containing the expected results. The code

Your code

def is_year_leap(year):
if year%4==0 and year%100!=0:
leap=True
elif year%400==0:
leap=True
else:
leap=False
return leap
print(is_year_leap(2000))
print(is_year_leap(2001))
test_data = [1900, 2000, 2016, 1987]
test_results = [False, True, True, False]
for i in range(len(test_data)):
yr = test_data[i]
print(yr,"->",end="")
result = is_year_leap(yr)
if result == test_results[i]:
print("OK")
else:
print("Failed")

def is_year_leap(year):
if year%4==0 and year%100!=0:
leap=True
elif year%400==0:
leap=True
elif year<1582:
leap=None
else:
leap=False
return leap
print(is_year_leap(1000))
print(is_year_leap(2000))
print(is_year_leap(2001))
test_data = [1900, 2000, 2016, 1987]
test_results = [False, True, True, False]
for i in range(len(test_data)):
yr = test_data[i]
print(yr,"->",end="")
result = is_year_leap(yr)
if result == test_results[i]:
print("OK")
else:
print("Failed")

PART III

Scenario: Your task is to write and test a function that takes two arguments (a year and a
month) and returns the number of days for the given month/year pair (while only February is
sensitive to the year value, your function should be universal).

The initial part of the function is ready. Now, convince the function to return None if its
arguments don't make sense. Of course, you can use the previously written and tested function in
part I (LAB 4.3.1.6). It may be very helpful. We encourage you to use a list filled with the
months' lengths. You can create it inside the function - this trick will significantly shorten the
code.

Your code
def is_year_leap(year):
if year%4==0 and year%100!=0:
leap=True
elif year%400==0:
leap=True
else:
leap=False
return leap
def days_in_month(year, month):
month_length = [31,28+is_year_leap(year),31,30,31,30,31,31,30,31,30,31]
if month<1:
return None
elif month>12:
return None
elif year<1:
return None
else:
return month_length [month-1]
print(days_in_month(2000, 2)) #has to return 29
print(days_in_month(2001, 2)) #has to return 28

Once you make sure the function is working correctly, use the below testing code.

Your code
def is_year_leap(year):
if year%4==0 and year%100!=0:
leap=True
elif year%400==0:
leap=True
else:
leap=False
return leap
def days_in_month(year, month):
month_length = [31,28+is_year_leap(year),31,30,31,30,31,31,30,31,30,31]
if month<1:
return None
elif month>12:
return None
elif year<1:
return None
else:
return month_length [month-1]
print(days_in_month(2000, 2)) #has to return 29
print(days_in_month(2001, 2)) #has to return 28
test_years = [1900, 2000, 2016, 1987]
test_months = [2, 2, 1, 11]
test_results = [28, 29, 31, 30]
for i in range(len(test_years)):
yr = test_years[i]
mo = test_months[i]
print(yr, mo, "->", end="")
result = days_in_month(yr, mo)
if result == test_results[i]:
print("OK")
else:
print("Failed")

Suggested Scoring Rubric

Possible Earned
Activity Section Points Points

Part 1: Program setup 25


Part 2: Code 50
Part 3: output verification 25
Total Packet Tracer Score 100

End of document REFERENCES: Cisco Network academy

You might also like