Assignment Unit3
Assignment Unit3
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)
def main():
num = int(input("Enter a number: "))
if num > 0:
countdown(num)
elif num < 0:
countup(num)
else:
print("Blastoff!")
if __name__ == "__main__":
main()
For an input of zero, both functions countdown and countup will result in
printing "Blastoff!" since they both check for values less than or equal to zero.
Therefore, the choice of which function to call for an input of zero is arbitrary.
In this implementation, I chose to call countdown, but you could choose
countup as well, as both functions produce the same output for zero.
Question 2
def divide_numbers():
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if num2 == 0:
raise ZeroDivisionError("Error: Division by zero is not allowed")
result = num1 / num2
print("Result of division:", result)
except ValueError:
print("Error: Please enter valid numbers")
except ZeroDivisionError as e:
print(e)
divide_numbers()