PYTHON LESSON
finally runs no matter what; raise triggers custom error.
Use finally for cleanup.
Raising vague generic errors.
This lesson is about finally and raise. You are in the Logic part of the course.
Main idea:
finally runs no matter what; raise triggers custom error.
How to learn it: type a very small program first. Run it. Change one line and run again.
Tip:
Use finally for cleanup.
Common mistake:
Raising vague generic errors.
Your challenge:
Validate age and raise ValueError if negative.
Scroll down to the challenge lab. Run your code there. When the output meets the task and the checks pass, press the green button to finish this topic.
Copy this example if it helps. Change it so it matches finally and raise.
# Topic: finally and raise
def main():
sample = "edit me"
# TODO: apply finally and raise concept here
result = sample
print("Result:", result)
if __name__ == "__main__":
main()
Validate age and raise ValueError if negative.
Before you can finish: your output should include at least 16 characters; at least 1 non-empty line(s); must contain (letters can be upper or lower case): "ValueError"; a ValueError message may appear (this lesson expects it).
Use Run. Read the output. Change your code until the task is done.
# Challenge starter for finally and raise
def solve():
# Write your solution here
pass
print("Update solve() and run")
The first Run may load Python in your browser (one-time). Later runs are faster.