CS1101-Unit03 programming assignment

docx

School

University of the People *

*We aren’t endorsed by this school

Course

1101

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

7

Uploaded by BaronEmuPerson965

Report
Q1. The code for my countup function: >>> def countup(n): ... if n >= 0: ... print('Blastoff!') ... else: ... print(n) ... countup(n+1) The output: >>> countup(-3) ... -3 ... -2 ... -1 ... Blastoff!
Program to run countdown or countup, depending on user keyboard input: Output (positive number): Output (negative number):
Output (zero): An explanation of your choice for what to call for input of zero. I chose to call countdown when user keyboard input is zero simply because it seemed most straightforward to me to make the determination by using if n >= 0 (if input is greater than or equal to 0, program runs countdown) and elif n < 0 (if input is less than 0, program runs countup). Q2.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Provide a code demonstrating how to handle the division by zero error. def division(): print("To perform division, please enter your dividend and divisor.") try: num1 = float(input("Dividend (number to be divided): ")) num2 = float(input("Divisor (number to divide by): ")) quotient = num1 / num2 print("Result =", quotient) except ZeroDivisionError: print("Error: Dividing by zero is not allowed!") division() division() In the division program I created, I have incorporated an except clause as described in the official Python Documentation ( 8. Errors and Exceptions , n.d., pt. 8.3 Handling Exceptions) to handle cases where the user attempts to divide by zero. In instances where a ZeroDivisionError is encountered, the program will print the explanatory error message, “Error: Dividing by zero is
not allowed!” Then, because the division function is recursive when the except clause is run, it will start over giving the user another chance to enter their numbers. Output demonstrating the runtime error, including the error message. Explain the significance of error handling in expressions or conditions, using the division by zero scenario as an example. Discuss the potential impact of not handling this error in a program. Please provide detailed explanations and code snippets to guide the junior developers in understanding and addressing the division by zero error in Python programs. Error handling is important because it enables a program to deal with unexpected conditions and avoid crashing. It also allows the code author to anticipate potential user mistakes and to provide meaningful feedback that a user can implement and correct how they are interacting with the program. If this error were not handled within the program, it would result in a generic runtime error. The runtime error may offer a user some information to the user about what went wrong, but it may be difficult for them to understand and will not give any specific suggestions about
what should be done differently. Handling the error within the program itself also allows the author to specify what happens after the error occurs (in this instance, restarting the program and requesting new user input,) while a generic runtime error would be followed by the abrupt termination of the program and no opportunity for the user to try different inputs.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
References 8. Errors and Exceptions . (n.d.). Python Documentation. https://docs.python.org/3/tutorial/errors.html