Question: [ HOMEWORK ] Problem # 1 . 1 The raise keyword is used to raise an exception. Write a Python code to raise Exception when

[HOMEWORK] Problem #1.1
The raise keyword is used to raise an exception. Write a Python code to raise Exception when an age is younger than 18 or older than 80. Code
a simple raise statement for the conditional statement; Then, complete a code segment to do the follo
HOMEWORK] Problem #1.2
{x}, Recall Example 1.1 above. Without changing the statement of the for-loops, improve the code of Example 1.1 to handle the exception(s).
A sample output is as follows:
89,85,21,77,87,62,64,16,91,96
e)89
85
21
77
87
62
64
16
91
96
list index out of range
[My Code 2:]
[] # code 2:
import random
# Create a list of random integers (based on your sample output, it looks like you have around 10 elements)
numbers random.randint(1,100) for _ in range(10)] # List of 10 random integers
print (numbers)
# Loop through the indices from to 10
for i in range(11): # This includes an extra index which will cause an IndexError
try:
print(f"{i}){numbers[i]}")wing:
Sample runs show below:Out of the bound
Enter your age: 101
Out of the bound
('Your age is ',55)
[My Code 1:]
[] Code 1:
if age 18 or age >88:whole code segment:
try:except Exception as e:
print(e)
[ HOMEWORK ] Problem # 1 . 1 The raise keyword is

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!