CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
267
You visited us 267 times! Enjoying our articles? Unlock Full Access!
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 the gender.

Open in App
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

flag
Suggest Corrections
thumbs-up
5
similar_icon
Similar questions
View More
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Introduction to Statistics
STATISTICS
Watch in App
Join BYJU'S Learning Program
CrossIcon