English

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 gender. - Computer Science (Python)

Advertisements
Advertisements

Question

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 gender.

Answer in Brief
Advertisements

Solution

Program:
#Defining a function which takes name and gender as input
def prefix(name,gender):
    if (gender == 'M' or gender == 'm'):
        print("Hello, Mr.",name)
    elif (gender == 'F' or gender == 'f'):
        print("Hello, Ms.",name)
    else:
        print("Please enter only M or F in gender")

#Asking the user to enter the name
name = input("Enter your name: ")

#Asking the user to enter the gender as M/F
gender = input("Enter your gender: M for Male, and F for Female: ")

#calling the function
prefix(name,gender)

OUTPUT:
Enter your name: John
Enter your gender: M for Male, and F for Female: M
Hello, Mr. John
shaalaa.com
  Is there an error in this question or solution?
Chapter 7: Functions - Exercise [Page 172]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 7 Functions
Exercise | Q 2. | Page 172
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×