Python - Guess Who game - Im not getting my expected output
<p>This is a small portion of my guess who game im developing that assigns the AI Opponents randomly chosen character with its linked features, and assigns those features to the feature variables. When the user asks the question the if statements will respond with a yes or no based on whether it has the features asked off, but i am not getting an expected output. I may not be able to reply with any further questions about this until i get back from college later today thanks.</p>

<p>I do not get any errors, but every response is a "No" even when the AI opponent 100% definetely has that feature. Heres the output im getting:</p>

<blockquote>
  <p>What is your question for your AI opponent? 
  Does your character have short hair? 
  AI Opponent: No
  What is your question for your AI opponent? 
  Does your character have long hair? 
  AI Opponent: No 
  What is your question for your AI opponent? 
  Is your character Bald? 
  AI Opponent: No 
  What is your question for your AI opponent? 
  Is your character male? 
  AI Opponent: No 
  What is your question for your AI opponent? 
  Is your character female? 
  AI Opponent: No</p>
</blockquote>

<pre><code>#This assigns a random character name to the variable 'AICharacterChoice'
        AICharacterChoice = random.choice(["Greg", "Chris", "Jason", "Clancy", "Betty", "Selena", "Helen", "Jacqueline"])
#This simpy defines these feature variables
        AIChoiceFeatureHairLength = "Unassigned"
        AIChoiceFeatureHairColour = "Unassigned"
        AIChoiceFeatureFacialHair = "Unassigned"
        AIChoiceFeatureJewellery = "Unassigned"
        AIChoiceFeatureHat = "Unassigned"
        AIChoiceFeatureLipstick = "Unassigned"
        AIChoiceGender = "Unassigned"
#This assigns the feature variables with features linked to that characters name
        if AICharacterChoice == "Greg":
            AIChoiceFeatureHairLength == "Short"
            AIChoiceFeatureHairColour == "Brown"
            AIChoiceFeatureFacialHair == "Yes"
            AIChoiceFeatureJewellery == "Yes"
            AIChoiceFeatureHat == "No"
            AIChoiceFeatureLipstick == "No"
            AIChoiceGender == "Male"

        if AICharacterChoice == "Chris":
            AIChoiceFeatureHairLength == "Long"
            AIChoiceFeatureHairColour == "Blonde"
            AIChoiceFeatureFacialHair == "No"
            AIChoiceFeatureJewellery == "No"
            AIChoiceFeatureHat == "Yes"
            AIChoiceFeatureLipstick == "Yes"
            AIChoiceGender == "Male"

        if AICharacterChoice == "Jason":
            AIChoiceFeatureHairLength == "Short"
            AIChoiceFeatureHairColour == "Brown"
            AIChoiceFeatureFacialHair == "Yes"
            AIChoiceFeatureJewellery == "No"
            AIChoiceFeatureHat == "Yes"
            AIChoiceFeatureLipstick == "No"
            AIChoiceGender == "Male"

        if AICharacterChoice == "Clancy":
            AIChoiceFeatureHairLength == "Bald"
            AIChoiceFeatureHairColour == "Red"
            AIChoiceFeatureFacialHair == "Yes"
            AIChoiceFeatureJewellery == "No"
            AIChoiceFeatureHat == "No"
            AIChoiceFeatureLipstick == "No"
            AIChoiceGender == "Male"

        if AICharacterChoice == "Betty":
            AIChoiceFeatureHairLength == "Bald"
            AIChoiceFeatureHairColour == "Blonde"
            AIChoiceFeatureFacialHair == "No"
            AIChoiceFeatureJewellery == "Yes"
            AIChoiceFeatureHat == "Yes"
            AIChoiceFeatureLipstick == "Yes"
            AIChoiceGender == "Female"

        if AICharacterChoice == "Selena":
            AIChoiceFeatureHairLength == "Long"
            AIChoiceFeatureHairColour == "Brown"
            AIChoiceFeatureFacialHair == "No"
            AIChoiceFeatureJewellery == "Yes"
            AIChoiceFeatureHat == "No"
            AIChoiceFeatureLipstick == "No"
            AIChoiceGender == "Female"

        if AICharacterChoice == "Helen":
            AIChoiceFeatureHairLength == "Short"
            AIChoiceFeatureHairColour == "Brown"
            AIChoiceFeatureFacialHair == "No"
            AIChoiceFeatureJewellery == "No"
            AIChoiceFeatureHat == "No"
            AIChoiceFeatureLipstick == "Yes"
            AIChoiceGender == "Female"

        if AICharacterChoice == "Jacqueline":
            AIChoiceFeatureHairLength == "Long"
            AIChoiceFeatureHairColour == "Red"
            AIChoiceFeatureFacialHair == "Yes"
            AIChoiceFeatureJewellery == "Yes"
            AIChoiceFeatureHat == "No"
            AIChoiceFeatureLipstick == "No"
            AIChoiceGender == "Female"
#This loops the questions to ask the AI opponent
        x = 1
        while x == 1:
#This asks the user what question they would like to ask the AI opponent, when they ask the question the if statements will reply with a "yes" or "no" based on whether is has that feature
            QuestionForAI = input("What is your question for your AI opponent? ").upper()
            if QuestionForAI == "DOES YOUR CHARACTER HAVE SHORT HAIR?" and "DOES YOUR CHARACTER HAVE SHORT HAIR":
                if AIChoiceFeatureHairLength == "Short":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")
            if QuestionForAI == "DOES YOUR CHARACTER HAVE LONG HAIR?" and "DOES YOUR CHARACTER HAVE LONG HAIR":
                if AIChoiceFeatureHairLength == "Long":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")
            if QuestionForAI == "DOES YOUR CHARACTER HAVE FACIAL HAIR?" and "DOES YOUR CHARACTER HAVE FACIAL HAIR":
                if AIChoiceFeatureHairColour == "Yes":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")
            if QuestionForAI == "IS YOUR CHARACTER MALE?" and "IS YOUR CHARACTER MALE":
                if AIChoiceGender == "Male":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")

            if QuestionForAI == "IS YOUR CHARACTER FEMALE?" and "IS YOUR CHARACTER FEMALE":
                if AIChoiceGender == "Female":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")

            if QuestionForAI == "DOES YOUR CHARACTER WEAR A HAT?" and "DOES YOUR CHARACTER WEAR A HAT":
                if AIChoiceFeatureHat == "Yes":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")

            if QuestionForAI == "DOES YOUR CHARACTER WEAR LIPSTICK?" and "DOES YOUR CHARACTER WEAR LIPSTICK":
                if AIChoiceFeatureLipstick == "Yes":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")

            if QuestionForAI == "DOES YOUR CHARACTER WEAR JEWELLERY?" and "DOESYOURCHARACTERWEARJEWELLERY?":
                if AIChoiceFeatureJewellery == "Yes":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")

            if QuestionForAI == "IS YOUR CHARACTER BLONDE HAIRED?" and "IS YOUR CHARACTER BLONDE HAIRED":
                if AIChoiceFeatureHairColour == "Blonde":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")

            if QuestionForAI == "IS YOUR CHARACTER BROWN HAIRED?" and "IS YOUR CHARACTER BROWN HAIRED":
                if AIChoiceFeatureHairColour == "Brown":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")

            if QuestionForAI == "IS YOUR CHARACTER BALD?" and "ISYOURCHARACTERBALD?":
                if AIChoiceFeatureHairLength == "Bald":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")

            if QuestionForAI == "IS YOUR CHARACTER RED HAIRED?" and "IS YOUR CHARACTER RED HAIRED":
                if AIChoiceFeatureHairColour == "Red":
                    print("AI Opponent: Yes")
                else:
                    print("AI Opponent: No")
</code></pre>

<p>You're not <em>assigning</em> inside the conditional name check bit:</p>

<pre><code>AIChoiceFeatureHairLength == "Short"
</code></pre>

<p>should be:</p>

<pre><code>AIChoiceFeatureHairLength = "Short"
</code></pre>

<p>Also - I would seriously think about reading up a bit on Object-Oriented Programming. If you make your characters an object, with some associated methods - it will save you a lot of typing. I'll try and put together something for you this morning to demonstrate.</p>

<hr>

<p>EDIT - as promised. I got a bit carried away learning about <a href="http://www.nltk.org/" rel="nofollow">NLTK</a>, so for my code to work, you'll have to make sure you've got NLTK on your python distribution (you will also need <a href="http://www.numpy.org/" rel="nofollow">numpy</a> if you don't already have it):</p>

<ol>
<li>Navigate to your <code>Scripts</code> folder (i.e. <code>C:\Python27\Scripts</code>) in a command prompt (NB: assuming that you're using Windows and Python 2.7 - if not, tell me and I'll adjust answer)</li>
<li>Then type <code>pip install nltk</code></li>
</ol>

<p>I've also used a simple statistical language parser <a href="https://github.com/emilmont/pyStatParser" rel="nofollow">pyStatParser</a>. To add this to python:</p>

<ol>
<li>Download the package from <a href="https://github.com/emilmont/pyStatParser" rel="nofollow">github</a></li>
<li>Unzip into your <code>Lib</code> folder of python (i.e. <code>C:\Python27\Lib</code>) - this is just good practice</li>
<li>Navigate to folder in cmd and run the file named <code>Setup.py</code> by typing <code>python Setup.py install</code></li>
</ol>

<p>This should be all the setup you need to get the code working. Installing and playing with these packages will make your game more interesting and challenging for you to develop (in my opinion). It should also make for a more 'updatable' game.</p>

<p>Here is my code. It sets up your <code>Character</code> as a <strong>class</strong>, which you can then make instances of (an <strong>object</strong>). Classes allow you to grow your code over time - whilst having minimal impact on the code already functioning correctly:</p>

<pre><code>from stat_parser import Parser
import nltk
parser = Parser()

class Character(object):
    def __init__(self, HairLen, HairCol, FacialHair, Jewel, Hat, Lipstick, Gender):
        self.gender = Gender.lower()
        self.hair = [HairLen.lower(), HairCol.lower()]
        if FacialHair.lower() == "yes":
            self.hair.append("facial")
        self.extras = []
        if Jewel.lower() == "yes":
            self.extras.append("jewellery")
        if Hat.lower() == "yes":
            self.extras.append("hat")
        if Lipstick.lower() == "yes":
            self.extras.append("lipstick")

    def answer(self, subject, adjective = ""):
#        print "subject, adj: ", subject, adjective
        subject = subject.lower()
        adjective = adjective.lower()
        if subject in ("male", "female"):
            return (subject == self.gender)
        elif subject == "hair":
            return (adjective in self.hair)
        elif subject in ("hat", "jewellery", "lipstick"):
            return (subject in self.extras)

def ask_question(question, character):
    pq = parser.parse(question)
    tokens = nltk.word_tokenize(question)
    tagged = nltk.pos_tag(tokens)
    start = ' '.join(tokens[:3])
    if start.lower() not in ("does your character", "is your character"):
        print "Error: Question needs to start with DOES/IS YOUR CHARACTER."
        ask_question(raw_input("Restate question: "))

    SQ = pq[0]
    if SQ.label() == "SQ":#on the right track
        if SQ[-1].label() == "VP": #verb phrase (i.e. 'have short hair')
            VP = SQ[-1].flatten()
            if VP[0] == "have":
                return character.answer(VP[2], VP[1])
            elif VP[0] == "wear":
                return character.answer(VP[-1])
        elif SQ[-1].label() == "ADJP": #adjective phrase (i.e. 'short haired')
            ADJP = SQ[-1].flatten()
            return character.answer(ADJP[1][:-2], ADJP[0]) #really hacky
        elif SQ[-1].label() == "NP": #noun phrase (i.e. 'your character female')
            NP = SQ[-1].flatten()
            if NP[-1].lower() == "bald": #special case
                return character.answer("hair", NP[-1])
            return character.answer(NP[-1])
    else:
        print "Error: Question not in correct form. Try something like:"
        print "    - Does your character have short hair?"
        print "    - Is your character short haired?"
        ask_question(raw_input("Restate question: "))

def question_loop(character):
    question = raw_input("Ask me a question (q to quit): ")
    if question != "q":
        print ask_question(question, Dave)
        question_loop(character)

#I'm calling __init__ here
Dave = Character("long", "blonde", "yes", "yes", "yes", "no", "male")

question_loop(Dave)
</code></pre>

<p>The class <code>Character</code> has two associated methods (special name for functions inside classes):</p>

<ol>
<li><code>__init__</code> - this is a special python function that <strong>initialises</strong> your object. It takes a number of arguments, the first once being <code>self</code>. It can then manipulate these parameters. In my version - I set up a number of internal variables.</li>
<li><code>answer</code> - this is called in the function <code>ask_question</code> (which takes your question text and a Character instance as arguments). When you call this function, you access it as a member function of the created object <code>Dave</code> by typing <code>character.answer(arg1, arg2)</code>. This automatically passes <code>self</code> (in this example; <code>character</code>) into the function. <code>answer</code> interrogates the variables set up in <code>__init__</code> and should return the answers to your questions.</li>
</ol>

<p>The code works on the test cases you included in your answer. As parsing natural language is complicated, you might have to tweek it to accept different forms of answers.</p>

<p>Let me know if you have any questions / problems. I hope this helps. I've certainly learnt some things in answering your question anyway.</p>

python output