0% found this document useful (0 votes)
5 views4 pages

Python Unit 5

The document describes two functions: countdown and countup, which handle counting down or up based on the input number, with countup specifically addressing negative numbers. It also includes a division function that checks for division by zero and provides error handling guidance for developers. Additionally, references are made to sources for understanding recursion and input handling.

Uploaded by

weisswesley22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python Unit 5

The document describes two functions: countdown and countup, which handle counting down or up based on the input number, with countup specifically addressing negative numbers. It also includes a division function that checks for division by zero and provides error handling guidance for developers. Additionally, references are made to sources for understanding recursion and input handling.

Uploaded by

weisswesley22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 1

The countup function checks if the argument is greater than or equal to zero and repeats until the condition is met

thus counting up negative numbers until the value of n becomes zero.

>>> def countdown(n):

... if n <= 0:

... print('Blastoff!')

... else:

... print(n)

... countdown(n-1)

...

>>> def countup(n):

... # Check if the value is greater than or equal to zero

... if n >= 0:

... # Print positive output if the condition is met

... print('Talk about a positive attitude😁')

... else:

... # Print the current value of n

... print(n)

... # Repeat the function with the value of n + 1

... countup(n + 1)Output

...

>>> def make_a_zero():


... print('Please input a number (FYI, not too big and not too small):')

... n = int(input()) # Convert input to an integer if a string or float is provided

... if n > 0:

... # Call the countdown function

... countdown(n)

... else:

... # Call the countup function

... countup(n)

...

Output

Note: In my make_a_zero function I chose to countup if the value of n is zero since when you have a zero the only
way to go is up. And you need a positive attitude to go up.

Question 2

The below function will allow the developer to run the function but if the divisor is zero the function will raise an

error with all the necessary information on how to prevent such a runtime error and its potential impact.

Code

>>> def division():


... print('Please enter the first number:')

... a = float(input("a = "))

... print('Please enter the second number:')

... b = float(input("b = "))

... if b == 0:

... # Provide an error message for junior developers

... print(

... "Error!: Division by zero detected. This runtime error can be avoided by ensuring "

... "that the denominator (second number) is checked before performing the division. "

... "For example:\n"

... "if b != 0:\n"

... " result = a / b\n"

... "else:\n"

... " print('Denominator cannot be zero.') \n"

... "Anytime you are making a program, it is important to think about exceptional cases "

... "like dividing something by zero or by a string.\n"

... "Forgetting to do so will result in runtime errors and lead the program to behave in unexpected ways.\n"

... "This prevents the correct functioning of the program and will give you a hard time debugging.\n"

... )

... elif a == 0:

... # Handle the case where the first number is zero


Information on recursion and if statements were derived from Downey, A. (2015). Think Python: How to
think like a computer scientist. Green Tree Press
... print("Result is 0 because any number divided by zero is 0.")

... else:

... # Perform the division if both numbers are valid

... print(f"The result of {a} / {b} is: {a / b}")

Output

Reference

Information on how to work efficiently with float and input functions was derived from Stack Overflow

[Link]

Allen Downey(2015)Think Python” 2nd Edition retrieved from


[Link]

You might also like