Professional Documents
Culture Documents
Programming Assignment Unit 03
Programming Assignment Unit 03
Code :
#The sys module that i'm using here in Python provides various functions and variables that are used to
manipulate different parts of the Python runtime environment. It allows operating on the interpreter as
it provides access to the variables and functions that interact strongly with the interpreter.
import sys
# Here's the count down function
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
def countup(n):
if n >= 0:
print('Blastoff!')
else:
print(n)
countup(n+1)
if sys.version_info[0] == 3:
else:
num = int(num)
if num > 0:
countup(num)
else:
print('Blastoff!')
Q.2
Code :
#First of all in Python, a ZeroDivisionError is raised when a division or modulo operation is attempted
with a denominator or divisor of 0.
b=0
print(a/b)#In this first block of code, a number a is attempted to be divided by another number b, whose
value is zero, leading to a ZeroDivisionError.
#How to Fix ZeroDivisionError in Python ? the ZeroDivisionError can be avoided using a conditional
statement to check for a denominator or divisor of 0 before performing the operation.
a = 10
b=0
if b == 0:
else:
print(a/b)
#NB: A try-except block can also be used to catch and handle this error if the value of the denominator is
not known beforehand:
try:
a = 10
b=0
print(a/b)
except ZeroDivisionError as e:
References: https://www.datacamp.com/tutorial/exception-handling-python