You are on page 1of 5

Q.

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)

#Here's the count up function

def countup(n):

if n >= 0:

print('Blastoff!')

else:

print(n)

countup(n+1)

#Here we a ask user to enter number

if sys.version_info[0] == 3:

num = input('Enter number: ')

else:

num = raw_input('Enter number: ')

#This line helps us to convert string to number

num = int(num)

if num > 0:

# This step assists us to count down positive number


countdown(num)

elif num < 0:

#And this step assists us count up negative number

countup(num)

else:

# there is no difference which function to call

# In this case of 0, both functions will print 'Blastoff!'

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.

#ZeroDivisionError in python is that A ZeroDivisionError occurs in Python when a number is attempted to


be divided by zero. Since division by zero is not allowed in mathematics, attempting this in Python code
raises a ZeroDivisionError.
a = 10

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.

#Here's an example of a ZeroDivisionError in Python caused by division by zero:

a = 10

b=0

if b == 0:

print("Cannot divide by zero")

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:

print("Error: Cannot divide by zero")

References: https://www.datacamp.com/tutorial/exception-handling-python

You might also like