Professional Documents
Culture Documents
1. Handle the exception thrown by the code below by using try and except blocks.
for i in
['a','b','c']:
print(i**2)
In [1]: try:
for i in ['a','b','c']:
print(i**2)
except:
print("You divided by zero, which is not allowed in my math!")
2. Write a function that asks for an integer and prints the square of it. Use a while loop with a try,
except, else block to account for incorrect inputs This should be the output of your function:
Input an integer: “hello”
Unrecognized number! Please try again!
Input an integer: 2
You
entered 2, your number squared is: 4
16
3. Complete the Line class given below. Your class should accept two sets of coordinate points
as
tuples. Write two methods within the class, one to compute distance between two points,
and
another to compute the slope.
You have to complete the code given below:
class Line:
def init(self,coordinateA,coordinateB):
pass
def distance(self):
pass
def slope(self):
pass
Example output:
c1 = (3, 2)
c2 = (8, 10)
(x1,x2) = (3, 2)
(y1,y2) = (8, 10)
c1=(x1,x2)
c2=(y1,y2)
myline = Line(c1, c2)
print(myline.distance())
print(myline.slope())
2.23606797749979
-2.0
class Cylinder:
def __init__(self,height=1,radius=1):
pass
def volume(self):
pass
def surface_area(self):
pass
3.141592653589793
12.566370614359172
5. Define a class InputOutString which has at least two methods: getString: to get a string from
console input, printString: to print the string in upper case. Also please include simple test
function
to test the class methods.
BIBEK
6. Write a python program to copy the contents of one file to another. Your new file should be
named as old file name followed by a 1. For example: if your original file is named: myfile.txt,
your
new file should be named: myfile1.txt
In [6]: file=open('myfile.txt',"r")
data=file.read()
file.close()
with open('myfile1.txt','a') as file:
file.write(data)
7.Write a lambda function that takes in two numbers, and returns the sum of the squares of the
input numbers.
Out[7]: 13
In [ ]: