0

This is a piece of code I was trying to make one day:

    proceed = 0
    print """Welcome to Magyck and Monsters
                A text-based RPG"""
    charactor_name = raw_input("What is your name? ")
    print "Welcome ", charactor_name
    while proceed == 0:
        gender_answer = raw_input("Are you male or female? ")
        if gender_answer == "male" or "MALE" or "mALE" or "Male":
            charactor_gender = "male"
            proceed = 1
        elif gender_answer == "Female" or "FEMALE" or "female" or "fEMALE":
            charactor_gender = "female"
            proceed = 1
        else:
            print "Sorry, I could not understand you."
            proceed = 0
    if proceed == 1:
        print "You are a ", + charactor_gender

It was supposed to be a text-based RPG game, the problem is, when i run it, no matter what I enter for the gender, I get get the printed message "You are a male" as if it overrided the first if statement and somehow made it true. The way it is supposed to work is that during a loop you would be asked a question, then check if the answer really made any sense and assign it a specific value that would not change. if the answer did not make any sense, it was supposed to say "Sorry, I could not understand you." and loop back to the question. I am fairly new to coding and would appreciate any input.

  • 2
    Not really related to the question but is there a reason you are learning Python2? Python3 is fairly mature now and I think they recommend using v3 unless there is a specific reason not to (some libraries are not yet available etc). – Holloway Mar 24 '15 at 22:59
  • I'm fairly new to python, as I said, and am using a book called "Hello World 2" to learn it. It uses python 2, so I do. When I am better at coding I will probably switch to python 3. – C. L. Holmes Mar 28 '15 at 14:03

2 Answers2

3

The line if gender_answer == "male" or "MALE" or "mALE" or "Male": will always be true.

What you are checking is if gender_answer equals "male" or if one of the other options are true where the other options are tested for their truthiness. As any non-empty string is True, the condition becomes gender_answer == "male" or True which will always be true.

Use if gender_answer in ("male", "Male", "MALE", "mALE"): instead or better yet if gender_answer.lower() == "male":.

Holloway
  • 6,412
  • 1
  • 26
  • 33
0

You need to make a complete condition.

if gender_answer == "male" or gender_answer=="MALE" or gender_answer=="mALE" or gender_answer=="Male":

Same solution for the elif.

Because "male" (or any string) is considered to be true, you go into the if. You need to compare it every times.

rm4
  • 711
  • 4
  • 15