Instructions
Course Outcome: 4. Explain the use of lists and dictionaries in Python programming
Course Outcome: 6. Summarize the use of files and exceptions in Python programming
“””
This is the flower box, and it should be at the beginning of each assignment.
You must add comments to your code
Program Name: Wk4_firstname_lastname.py
Student Name: Ymmas Azaba
Course: ENTD220
Instructor: My instructor
Date: Any Day
Copy Wrong: This is my work.
“””
ASSIGNMENT
You are going to enhance the prior assignment by implementing exceptions!
***** You must tell me where you implemented the exception and why. *****
- Show me 2 test cases that will trap the errors.
- Sample output
- Enter first number between -10 and 20 –> add
- Error –> could not convert string to float: 'add' <<<< first trap
- Enter first number between -10 and 20 –> 10
- Enter second number between -10 and 20 –> 0
- Error –> float division by zero <<< second trap
Submission Instructions:
- Make sure that you save your code in a text file in this format.
- W4_firstname_lastname.py
HINTS:
"""
comment block
"""
# def functions
def add …
def sub…
def …
def isinrange() or checknum()
while True:
try:
## Get user inputs
## Do calculation and print results
except Exception as e:
print("Error –>",e)
continue
print thank you for using…
# Trapping Errors
# Trapping error to avoid program breaking when error occurs
# errors can be user errors or system errors
# try-except should be on the program level
# try-except gives the user a chance to correct
# the error instead of terminating the program
"""
Do I raise or return errors in Python?
Raise a python exception, and then wrap all of your self written functions in a try/except wrapper.
Return a result that indicates there was an error. As easy as returning False if it didn't work,
or returning a dictionary with an error field. What's the pro's and con's of each? Thoughts?
Never return an error in the same place as a value.
https://www.w3schools.com/python/gloss_python_raise.asp
https://www.programiz.com/python-programming/exception-handling
"""
def div(n1,n2):
return n1/n2
while True:
try:
fnum=float(input("Enter first number –> "))
snum=float(input("Enter second number –> "))
print("The div = ", div(fnum,snum));
break
except Exception as e:
print(“Error: “,e)
continue
Enter first number –> sam
ValueError: could not convert string to float: 'sam'
# Test 2
Enter first number –> 7
Enter second number –> 0
Would you like to discuss this project or get it done?
Reach out on WHATSapp at +1 (240) 389-5520
Or
Place an order on our website for quick help
Guarantees
A+ Paper
Timely Delivery
Zero Plagiarism
Zero AI

Leave a Reply