CSCI101 - Lab08 - Functions Zewail City
CSCI101 - Lab08 - Functions Zewail City
1. What is wrong with the following function? Suggest how to fix it.
The function is supposed to calculate and return the sum of items of a list x.
def myfun1(x)
x = input(“Enter List x:”).split()
s=0
for i in range(1,11):
s = s+x[i]
return y
2. What will be the return of foo1(5) given the following function definitions?
def foo1(x)
a=foo2(x*2)
return a
def foo2(x)
y= x+5
return y
3.
a. The following function takes an angle in degrees and returns angle type
(zero, acute, right, obtuse, straight, reflex, or complete ). Complete the
function code
c. Function showResult takes the angle before and after conversion and
prints on the screen a message like:
“Angle of 130 degree = 2.27 rad”
Hint: This function needs 2 inputs, but does not return a value.
Write all the functions first then write the main script.
Note that the solution to this problem involves four code files: one which
acts as a main program (the script shown below), and three for the
functions. Import the function module into your main script.
University of Science and Technology
Communications & Information Engineering Program
CSCI 101: Introduction to Computer Science
Functions (Modular Programming)
8. Write a program that calls a function in the main script to reverse a string. Your
function is in the same script file of the main script.
Example:
Insert your string: ‘1234abcd’
The reversed string is: ‘dcba4321’
Hint: you may need to use join function, for example:
My_list = [‘H’, ‘e’, ‘l’, ‘l’, ‘o’]
x = “#”.join(My_list)
print(x)
>>> ‘H#e#l#l#o’
Hint: We did this problem before, but here we need to make a function that
reads a given string and returns the reversed one.
9. Every five years, we have an extra day so that February becomes 29 days. The
year with an extra day is called a “Leap Year”.