Professional Documents
Culture Documents
07 PGM
07 PGM
import os
import sys
import pathlib
import zipfile
if not os.path.isdir(dirName):
print("Directory", dirName, "doesn't exists")
sys.exit(0)
curDirectory = pathlib.Path(dirName)
if os.path.isfile("myZip.zip"):
print("Archive", "myZip.zip", "created successfully")
else:
print("Error in creating zip archive")
8. Write a function named DivExp which takes TWO parameters a, b and returns a value c
(c=a/b).
Write suitable assertion for a>0 in function DivExp and raise an exception for when b=0.
Develop a
suitable program which reads two values from the console and calls a function DivExp.
Program:
def DivExp(a, b):
assert a > 0, "Error: 'a' should be positive."
if b == 0:
raise Exception("Error: Division by zero.")
c=a/b
return c
OUTPUT
1) Enter value for 'a': 3
Enter value for 'b': 2
Result: 1.5
class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag
def __str__(self):
return '{} + {}i'.format(self.real, self.imag)
def add_complex_numbers(complex_numbers):
result = Complex(0, 0)
for number in complex_numbers:
result += number
return result
output:
Enter the number of complex numbers: 2
Enter real part of complex number 1: 4
Enter imaginary part of complex number 1: 2
Enter real part of complex number 2: 5
Enter imaginary part of complex number 2: 3
Result of addition: 9.0 + 5.0i