हिंदी

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

प्रश्न

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.

संक्षेप में उत्तर
Advertisements

उत्तर

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 7: Functions - Exercise [पृष्ठ १७२]

APPEARS IN

एनसीईआरटी Computer Science [English] Class 11
अध्याय 7 Functions
Exercise | Q 2. | पृष्ठ १७२
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×