NCERT Computer Science Class #11 – Chapter #7 Solutions – Q #9

Chapter #7 – Functions

NCERT Computer Science Class #11 – Chapter #7 Solutions – Lab Exercises #2

Chapter #7 – Lab Exercises #2:

Write a program that uses a user defined function that accepts name and gender (as M for Male, F for Female) and prefixes Mr/Ms on the basis of the gender.



def prefix_name(name, gender):
    """
    Prefix the name with Mr. or Ms. based on the gender.

    Parameters:
    name (str): The name to be prefixed.
    gender (str): The gender ('M' for Male, 'F' for Female).

    Returns:
    str: The prefixed name.
    """
    if gender.upper() == 'M':
        return f"Mr. {name}"
    elif gender.upper() == 'F':
        return f"Ms. {name}"
    else:
        return "Invalid gender input. Please use 'M' for Male or 'F' for Female."

# User input
name_input = input("Enter your name: ")
gender_input = input("Enter your gender (M/F): ")

# Get the prefixed name
result = prefix_name(name_input, gender_input)

# Display the result
print(result)

You can checkout the code from Github here – Lab Exercises – 2

How the Program Works :

1.	Function Definition: The prefix_name function checks the gender parameter:
      • If it is 'M' (or 'm'), it prefixes the name with "Mr."
      •	If it is 'F' (or 'f'), it prefixes the name with "Ms."
      •	If the input is neither, it returns an error message indicating invalid 
        input.

2.	User Input: The program prompts the user to enter their name and gender.

3.	Result Display: The program calls the prefix_name function with the user's 
        inputs and prints the prefixed name or an error message if the gender input 
        is invalid.

Example Output:

Enter your name: Alice
Enter your gender (M/F): F
Ms. Alice

Another Example:

Enter your name: Bob
Enter your gender (M/F): M
Mr. Bob

NCERT Computer Science Class 11 Chapter 7 Solutions Q 9

Posts created 29

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top