Write a program in Python to check if the user is eligible to marry.
★ Hints :
(i) The minimum age for males for marriage is 21 years.
(ii) The minimum age for females for marriage is 18 years.
★ Points to note :-
(i) Program should ask the user to enter his/her important details.
(ii) It's mandatory for the program to ask the gender and age of the user.
AIl the very best !
Answers
Required :
gender=input("Enter your gender\n'M' for male\n'F' for female\n")
age=(int)(input("Enter your age: "))
print ("Eligible" if((age>=18 and gender=='F')or(age>=21 and gender == 'M')) else "Not eligible")
Logic:-
- Ask gender and age as input
- Check if age and gender are >=18 and female respectively or >=21 and male respectively
- yes? Eligible
- otherwise not eligible
Sample I/O:-
Enter your gender
'M' for male
'F' for female
M
Enter your age: 18
Not eligible
Enter your gender
'M' for male
'F' for female
M
Enter your age: 27
Eligible
Enter your gender
'M' for male
'F' for female
F
Enter your age: 17
Not eligible
Enter your gender
'M' for male
'F' for female
F
Enter your age: 30
Eligible
Eligible to marry - Python
Before we write the required program for this question, let's first take some hint for better understanding and know that what we need to do in this program and how to write the program.
⠀
_____________...
What we need to do
We would take a user integer input first to accept the name from the user. We know that the input() function returns a string, string is one of the basis types in Python that store text.
⠀
After that same as name we will also take a user input to accept the gender from the user. We can accept the gender from the user by simply typing input() function.
⠀
After that, we would take a user integer input to accept the age from the user. We know that when we want integer value from the user, we always use int() function to accept the integer value from the user.
⠀
After this we will check the criteria that user is male or female. If the user is male then we will display the messege whether user is eligible for marriage or not.
⠀
And if the user is female then we will display the messege whether user is eligible for marriage or not.
⠀
Now, if the user enter invalid gender then we will display the required messege to the user to try again and enter valid gender.
⠀
_____________...
The Program
name=input("Enter your name: ")
gender=input("Enter your gender: ").lower()
age=int(input("Enter your age: "))
if (gender in ("male","m")):
if(age >= 21):
print("Congratulations dear", name, "you are eligible for marriage.")
else:
print("Sorry to say dear", name,"you are not eligible for marriage.")
elif (gender in ("female","f")):
if(age >= 18):
print("Congratulations dear", name, "you are eligible for marriage.")
else:
print("Sorry to say dear", name,"you are not eligible for marriage.")
else:
print("This is not valid gender, kindly enter valid gender and try again. Thanks!")
_____________...
Sample run
For male
Case one for male:
Enter your name: Eldarion
Enter your gender: Male
Enter your age: 25
Congratulations dear Eldarion you are eligible for marriage.
⠀
Case two for male:
Enter your name: Eldarion
Enter your gender: Male
Enter your age: 19
Sorry to say dear Eldarion you are not eligible for marriage.
⠀
Case three for male:
Enter your name: Eldarion
Enter your gender: M
Enter your age: 25
Congratulations dear Eldarion you are eligible for marriage.
⠀
Case four for male:
Enter your name: Eldarion
Enter your gender: kk
Enter your age: 25
This is not valid gender, kindly enter valid gender and try again. Thanks!
⠀
For female
Case one for female:
Enter your name: Sophia
Enter your gender: Female
Enter your age: 20
Congratulations dear Sophia you are eligible for marriage.
⠀
Case two for female:
Enter your name: Sophia
Enter your gender: Female
Enter your age: 15
Sorry to say dear Sophia you are not eligible for marriage.
⠀
Case three for female:
Enter your name: Sophia
Enter your gender: F
Enter your age: 20
Congratulations dear Sophia you are eligible for marriage.
⠀
Case four for female:
Enter your name: Sophia
Enter your gender: ss
Enter your age: 20
This is not valid gender, kindly enter valid gender and try again. Thanks!