Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using Python 3.6 I have written the following code to fulfill certain parameters

ID: 3593860 • Letter: U

Question

using Python 3.6

I have written the following code to fulfill certain parameters, the parameters are

Define a function named "talk" that accepts the following parameters:

name - required

age - optional parameter - default value should be 21

occupation - required parameter

In the body of the function do the following:

Make sure the name contains no numeric digits (0-9) - If not raise Exception

Make sure the age is not a negative value or over 150 - If not raise Exception

If the first letter in the name is between 'A' and 'M' then return a string result of 'Group 1'

If the first letter in the name is between 'N' and 'Z' then return a string result of 'Group 2'

Explanation / Answer

You should not catch exception in the function itself. Corrected your code

class Error(Exception):
pass

class NameContainsNumber(Error):
pass
class AgeOutOfRange(Error):
pass

def talk(name, occupation, age=21):
ifNameContainsNumber = any(char.isdigit() for char in name)
if ifNameContainsNumber:
raise NameContainsNumber
if (age<0 or age>150):
raise AgeOutOfRange
if name[0]>='A' and name[0]<= 'M':
return "Group 1"
if name[0]>= 'N' and name[0]<='Z':
return "Group 2"

try:
talk('MyName123', 'ocuupation', 21)
print("talk function failed as it didn't raise exception for not having a number")
except NameContainsNumber:
print("talk function pass test to raise exception for not having number")

try:
talk('MyName', 'ocuupation', -1)
print("talk function failed as it didn't raise exception for age less than 0")
except AgeOutOfRange:
print("talk function pass test to raise exception for age less than 0")

try:
talk('MyName', 'ocuupation', 160)
print("talk function failed as it didn't raise exception for age greater than 150")
except AgeOutOfRange:
print("talk function pass test to raise exception for age age greater than 150")
  
if talk('MyName', 'occumeation') == 'Group 1':
print("talk function pass test for group1")
else:
print("talk function failed test for group1")

if talk('NewName', 'occumeation') == 'Group 2':
print("talk function pass test for group2")
else:
print("talk function failed test for group2")

# code link: https://paste.ee/p/EaOXC

Sample execution: