When debugging your code, which Python keyword will test an object for specific conditions and will raise an error and stop the program if the condition tests false?
Added by Yolanda F.
Step 1
--- ** Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 65 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Scenario You are testing code to determine types of errors. During programming, errors often occur. But how can we anticipate when something will cause an error? You can anticipate errors by thoroughly understanding error scenarios and causes. Aim In this activity, we will create error scenarios. Let's write some code that will cause a KeyError and an AttributeError. Steps for Completion 1. Go to your main.py file. 2. Define a function createError to first generate a KeyError and then an AttributeError. 3. In order to generate both errors, you will need to generate only one error at a time. Grading Complete each task listed below. Each task contains automated checks which are used to calculate your grade. When you have completed each task by clicking the checkbox, open the task list panel on the left navigation bar and click the "Submit" button. To generate a KeyError: 1. Create the dictionary shown in Snippet 9.29: dining = dict( name = "Johnny's Restaurant", location = "Times Square" ) Snippet 9.29 Write a print statement that uses a key that's not defined in this dictionary. Running the script with python3 main.py should result in only one console error similar to the example below: KeyError: 'address' Task Modify main.py to create a KeyError. To generate an AttributeError: 1. Import the string module. 2. Use an attribute that's not defined in the string module. Running the script with python3 main.py should result in only one console error similar to the example below: AttributeError: module 'string' has no attribute 'example
Akash M.
Identifying Error Scenarios Scenario You are testing code to determine types of errors. During programming, errors often occur. But how can we anticipate when something will cause an error? You can anticipate errors by thoroughly understanding error scenarios and causes. Aim In this activity, we will create error scenarios. Let's write some code that will cause a KeyError and an AttributeError. Steps for Completion 1. Go to your main.py file. 2. Define a function createError to first generate a KeyError and then an AttributeError. In order to generate both errors, you will need to generate only one error at a time. Grading Complete each task listed below.
Create your own example of each possibility in Python code. List the code for each example, along with sample output from trying to run it. There is something wrong with the arguments the function is getting; a precondition is violated. • There is something wrong with the function; a postcondition is violated. • There is something wrong with the return value or the way it is being used.
Madhur L.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD