Python - 猜猜谁是游戏 - 我没有得到我预期的输出

时间:2016-10-14 08:14:21

标签: python output

这是我猜的一小部分我正在开发的游戏,它将AI Opponents随机选择的角色与其链接的特征分配,并将这些特征分配给特征变量。当用户询问问题时,if语句将根据是否有要求关闭的功能回答是或否,但我没有得到预期的输出。在我今天晚些时候回到大学之前,我可能无法回答任何有关此问题的任何问题,谢谢。

我没有得到任何错误,但即使AI对手100%定义具有该功能,每个响应都是“否”。继续得到输出:

  

你的AI对手有什么问题?   你的角色有短发吗?   AI对手:不   你的AI对手有什么问题?   你的角色有长发吗?   AI对手:不   你的AI对手有什么问题?   你的角色是秃头?   AI对手:不   你的AI对手有什么问题?   你的角色是男性吗?   AI对手:不   你的AI对手有什么问题?   你的角色是女性吗?   AI对手:否

#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")

2 个答案:

答案 0 :(得分:1)

您未在条件名称检查位中分配

AIChoiceFeatureHairLength == "Short"

应该是:

AIChoiceFeatureHairLength = "Short"

另外 - 我会认真考虑阅读面向对象编程。如果你使用一些相关的方法使你的角色成为一个对象 - 它将为你节省大量的输入。今天早上我会尝试为你准备一些东西来展示。

编辑 - 正如所承诺的那样。我有点忘记了解NLTK,所以为了让我的代码工作,你必须确保你的python发行版上有NLTK(你还需要numpy如果你还没有它):

  1. 在命令提示符中导航到您的Scripts文件夹(即C:\Python27\Scripts)(注意:假设您正在使用Windows和Python 2.7 - 如果没有,请告诉我和我' ll调整答案)
  2. 然后输入pip install nltk
  3. 我还使用了一个简单的统计语言解析器pyStatParser。将其添加到python:

    1. github
    2. 下载该软件包
    3. 解压缩到你的Lib python文件夹(即C:\Python27\Lib) - 这只是一个很好的做法
    4. 导航到cmd中的文件夹,然后输入Setup.py
    5. 运行名为python Setup.py install的文件

      这应该是使代码正常工作所需的所有设置。安装和玩这些包将使您的游戏更有趣,更具挑战性(在我看来)。它还应该有一个更可更新的'游戏。

      这是我的代码。它会将您的Character设置为,然后您可以创建(对象)的实例。类允许您随着时间的推移增加代码 - 同时对已正常运行的代码的影响最小:

      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)
      

      Character有两个相关的方法(类中函数的特殊名称):

      1. __init__ - 这是一个特殊的python函数,初始化你的对象。它需要一些参数,第一个是self。然后它可以操纵这些参数。在我的版本中 - 我设置了许多内部变量。
      2. answer - 在函数ask_question中调用它(它将您的问题文本和一个Character实例作为参数)。调用此函数时,可以通过键入Dave将其作为创建的对象character.answer(arg1, arg2)的成员函数进行访问。这会自动将self(在此示例中为character)传递到函数中。 answer询问__init__中设置的变量,并应返回您问题的答案。
      3. 代码适用于您在答案中包含的测试用例。由于解析自然语言很复杂,您可能需要调整它以接受不同形式的答案。

        如果您有任何疑问/问题,请告诉我。我希望这有帮助。无论如何,我在回答你的问题时肯定学会了一些东西。

答案 1 :(得分:0)

TransformedRectangle是条件语句,它将评估为AIChoiceFeatureHairLength == "Short"True

您想要做的是这样的任务:False

我还建议使用如下字典:

AIChoiceFeatureHairLength = "Short"

您可以将字典放入列表(Greg = {"HairLength":"Short", "HairColour":"Brown", "FacialHair":"Yes", "Jewellery":"Yes", "FeatureHat":"No", "Lipstick":"No","Gender":"Male"}` )并使用[Greg, Chris, etc]函数。

您可以使用random.choice()访问字典中的条目,并返回Greg["HairLenght"]

这将大大缩短代码,您也不需要将变量引入"Short"