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

Assignment 3 - Functions & Exceptions

This document provides instructions for Assignment 3 on functions and exceptions in Python for a course on advanced programming and customizing GIS. The assignment consists of 5 compulsory questions to help students practice defining functions, passing parameters, returning results, and handling errors. Students are instructed to write functions to calculate triangle hypotenuses from side lengths, shipping charges based on number of items, days in a given month, convert between hexadecimal and decimal, and identify functions without comments. Students must submit their work as a PDF file with their name and registration number.

Uploaded by

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

Assignment 3 - Functions & Exceptions

This document provides instructions for Assignment 3 on functions and exceptions in Python for a course on advanced programming and customizing GIS. The assignment consists of 5 compulsory questions to help students practice defining functions, passing parameters, returning results, and handling errors. Students are instructed to write functions to calculate triangle hypotenuses from side lengths, shipping charges based on number of items, days in a given month, convert between hexadecimal and decimal, and identify functions without comments. Students must submit their work as a PDF file with their name and registration number.

Uploaded by

Aneeqaa Abrar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

INSTITUTE OF SPACE TECHNOLOGY

DEPARTMENT OF SPACE SCIENCE


RG731- ADVANCED PROGRAMING AND CUSTOMIZING GIS
Batch & Section:
Student Registration No.:
Student Full Name:
 Cheating by all means is prohibited and any such attempt may lead to serious consequences.

Assignment 3 – Functions & Exceptions


This assignment is designed to help you develop your analysis skills by providing you with the
opportunity to practice breaking small problems down into sequences of steps. The assignment consists
of 5 compulsory questions.

The questions are designed to help you become familiar with Python’s functions. Writing functions is
often an important part of developing larger pieces of software. To complete each question, you must
do a dry run. You should expect to use some or all of the following Python features:

 Define a function for later use


 Pass one or more values into a function
 Perform a complex calculation within a function
 Return one or more results from a function
 Call a function that you have defined previously
 Detect and recover from errors
 Add enough comments

After you have completed the assignment, you need to submit the file in a .pdf format. The file you
submit should have an appropriate file type. The file should include your name and your registration
number e.g., abc_123456789_assignment3.pdf -- would be an appropriate filename for a student with
name abc and 123456789 that was submitting a file for homework assignment 3.

We expect students to not look at implementations online and do the assignment themselves.
Plagiarism will be dealt with strictly.

Late submissions will be penalised.

Spring 2021 Page 1 of 2


Questions [15 Marks]
1. Write a function that takes the lengths of the two shorter sides of a right triangle as its parameters.
Return the hypotenuse of the triangle, computed using Pythagorean theorem, as the function’s
result. Include a main program that reads the lengths of the shorter sides of a right triangle from the
user, uses your function to compute the length of the hypotenuse, and displays the result.
2. An online retailer provides express shipping for many of its items at a rate of $10.95 for the first
item, and $2.95 for each subsequent item.
Write a function that takes the number of items in the order as its only parameter. Return the
shipping charge for the order as the function’s result. Include a main program that reads the number
of items purchased from the user and displays the shipping charge.
3. Write a function that determines how many days there are in a particular month. Your function will
take two parameters: The month as an integer between 1 and 12, and the year as a four-digit
integer. Ensure that your function reports the correct number of days in February for leap years.
Include a main program that reads a month and year from the user and displays the number of days
in that month.
4. Write two functions, hex2int and int2hex, that convert between hexadecimal digits (0, 1, 2, 3, 4, 5, 6,
7, 8, 9, A, B, C, D, E and F) and base 10 integers. The hex2int function is responsible for converting a
string containing a single hexadecimal digit to a base 10 integer, while the int2hex function is
responsible for converting an integer between 0 and 15 to a single hexadecimal digit.
Each function will take the value to convert as its only parameter and return the converted value as
the function’s only result. Ensure that the hex2int function works correctly for both uppercase and
lowercase letters. Your functions should end the program with a meaningful error message if an
invalid parameter is provided.
5. When one writes a function, it is generally a good idea to include a comment that outlines the
function’s purpose, its parameters and its return value. However, sometimes comments are
forgotten, or left out by well-intentioned programmers that plan to write them later but then never
get around to it.
Create a python program that reads one or more Python source files and identifies functions that
are not immediately preceded by a comment. For the purposes of this question, assume that any
line that begins with def, followed by a space, is the beginning of a function definition. Assume that
the comment character, #, will be the first character on the previous line when the function has a
comment. Display the names of all of the functions that are missing comments, along with the file
name and line number where the function definition is located. The user will provide the names of
one or more Python files as command line parameters. If your program encounters a file that
doesn’t exist or can’t be opened, then it should display an appropriate error message before moving
on and processing the remaining files.

Spring 2021 Page 2 of 2

You might also like