How should I approach this program? I tried first part with AND and wrote 3 if statements

Viewed 42

I'm trying to pick up programming and I'm stuck with this one how should I resolve it - any good hints?

I need to create a program that checks if the candidate fits into the team. The age must be between 16 and 18 and only male is allowed.

Upgrade the program so that if the candidate is a female, no age will be asked at all

2 Answers
gender = input("Enter M or F:")
if(gender == 'M' or gender == 'm'):
 age = int(input("Enter your age"))
 if(age>=16 and age<=18):
   print("Allowed")

A much shorter way of writing this would be:-

if(input("Enter M or F") in ['m','M']):
 if(int(input('Enter age')) in range(16:19))

First things first, one of those questions clearly needs to be asked immediately, since it doesn't rely on anything else. You'll have to figure that out. After that question has been answered, then a follow-up question needs to be answered. Try ordering the questions in different ways to figure out how to do it correctly.

It might also help to think of how you would sort through a small group of random people.

Related