You are on page 1of 4

4/12/22, 4:02 PM Assignment 3rd - Jupyter Notebook

Name: Bivek Kumar Sah

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!")

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

In [2]:  def Sqr():


while True:
try:
number=int(input("enter the number:"))
square=number**2
print(square)
break
except:
print("Unrecognized number! Please try again! Input an integer:")
Sqr()

enter the number: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

localhost:8888/notebooks/Downloads/Assignment 3rd.ipynb 1/4


4/12/22, 4:02 PM Assignment 3rd - Jupyter Notebook

Example output:

c1 = (3, 2)

c2 = (8, 10)

myline = Line(c1, c2)

In [3]:  class Line:


def __init__(self,coordinateA,coordinateB):
self.A=coordinateA
self.B=coordinateB
def distance(self):
return ((x1 - x2)**2 + (y1 - y2)**2)**0.5
def slope(self):
return ((y2-y1)/(x2-x1))

(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

4. Complete the class given below:

class Cylinder:

def __init__(self,height=1,radius=1):

pass

def volume(self):

pass

def surface_area(self):

pass

localhost:8888/notebooks/Downloads/Assignment 3rd.ipynb 2/4


4/12/22, 4:02 PM Assignment 3rd - Jupyter Notebook

In [4]:  import math


pi=math.pi
class Cylinder:

def __init__(self,height=1,radius=1):
self.h=height
self.r=radius
def volume(self):
return (math.pi*(self.r*self.r)*self.h)

def surface_area(self):
return (2*math.pi*self.r*(self.r+self.h))

obj=Cylinder()
print(obj.volume())
print(obj.surface_area())

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.

In [5]:  class InputOutString():


def get_String(self):
self.str1 = input("Enter the string:")

def print_String(self):
print(self.str1.upper())

str1 = InputOutString()
str1.get_String()
str1.print_String()

Enter the string:bibek

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.

localhost:8888/notebooks/Downloads/Assignment 3rd.ipynb 3/4


4/12/22, 4:02 PM Assignment 3rd - Jupyter Notebook

In [7]:  square=lambda x,y:x**2+y**2


square(2,3)

Out[7]: 13

In [ ]: 

localhost:8888/notebooks/Downloads/Assignment 3rd.ipynb 4/4

You might also like