1

I'm trying to write a script that takes in a character (M) OR (F) and returns output as male OR female. I was wondering how I could do this with a while loop. What I have currently is if statements which do not loop back.

gender = input('What is your gender? M/F: ').capitalize()
mGender = ''
if gender == 'M':
   mGender = 'Male'
elif gender == 'F':
   mGender = 'Female'
else:
   #how do I go back to make sure only F/f or M/m were input by the user?
1
  • ...in the end the output should be either male or female; print ( 'Your gender is ' + mGender) Commented Dec 18, 2019 at 8:29

1 Answer 1

2

You are trying to do when user input M/m or F/f program display male/female. If you enter any other character system should display "please enter gender again message" and user asked again what is your gender. You can try this

gender = input('What is your gender? M/F: ').capitalize()
mGender = ''

while gender != 'M' or gender != 'F':
    if gender == 'M':
        mGender = 'Male'
        break;
    elif gender == 'F':
        mGender = 'Female'
        break;
    else:
        print("Wrong input please enter gender again")
        print()
        gender = input('What is your gender? M/F: ').capitalize()

print('Your gender is = ' +mGender)

In this code If you enter wrong character system ask your gender again and again until you enter M/m or F/f.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm, this does help to shed light on my own understanding of while loops. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.