首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python -猜猜谁的游戏-我没有得到我的预期输出

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

Stack Overflow用户
提问于 2016-10-14 08:14:21
回答 2查看 1.7K关注 0票数 0

这是我猜我开发的游戏中的一小部分,它将AI对手随机选择的字符与其链接的特征,并将这些特征分配给特征变量。当用户问这个问题时,if语句将根据它是否有被问到的特性来回答是或否,但是我没有得到预期的输出。在我今天晚些时候从大学回来之前,我可能无法回答关于这方面的任何进一步问题,谢谢。

我没有任何错误,但每个回应都是一个“否”,即使AI对手100%肯定有这个功能。这是我得到的输出:

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

代码语言:javascript
复制
#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")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-14 08:23:51

您没有在条件名称检查位内分配:

代码语言:javascript
复制
AIChoiceFeatureHairLength == "Short"

应:

代码语言:javascript
复制
AIChoiceFeatureHairLength = "Short"

另外,我会认真考虑阅读一些关于面向对象编程的内容。如果你把你的字符变成一个对象,用一些相关的方法--它会为你节省大量的打字。今天早上我会给你们准备些东西演示一下。

编辑-按照承诺。我对NLTK的了解有点过头了,所以我的代码要想工作,就必须确保您已经在您的python发行版上使用了NLTK (如果您还没有它的话,还需要numpy ):

  1. 在命令提示符中导航到您的Scripts文件夹(即C:\Python27\Scripts) (NB:假设您使用的是WindowsandPython2.7-如果不是,请告诉我,我将调整答案)
  2. 然后键入pip install nltk

我还使用了一个简单的统计语言解析器pyStatParser。要将其添加到python中,请执行以下操作:

  1. github下载软件包
  2. 解压缩到python的Lib文件夹(即C:\Python27\Lib) --这只是一个很好的实践
  3. 导航到cmd中的文件夹并通过键入Setup.py运行名为python Setup.py install的文件

这应该是使代码正常工作所需的全部设置。安装和玩这些软件包将使您的游戏更有趣和更具挑战性的发展(在我看来)。它也应该成为一个更“可更新”的游戏。

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

代码语言:javascript
复制
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中调用的(它以问题文本和字符实例作为参数)。调用此函数时,可以通过键入Dave作为创建的对象character.answer(arg1, arg2)的成员函数访问它。这会自动将self (在本例中为character)传递到函数中。answer询问在__init__中设置的变量,并且应该返回问题的答案。

该代码适用于您在回答中包含的测试用例。由于解析自然语言是复杂的,您可能不得不每周接受不同形式的答案。

如果你有任何问题/问题请告诉我。我希望这能帮到你。我在回答你的问题时确实学到了一些东西。

票数 1
EN

Stack Overflow用户

发布于 2016-10-14 09:16:14

AIChoiceFeatureHairLength == "Short"是一个条件语句,它将计算为TrueFalse

您想要做的是这样一个指定:AIChoiceFeatureHairLength = "Short"

我还建议使用字典,例如:

代码语言:javascript
复制
Greg = {"HairLength":"Short", "HairColour":"Brown", "FacialHair":"Yes", "Jewellery":"Yes", "FeatureHat":"No", "Lipstick":"No","Gender":"Male"}`

您可以将字典放在一个列表([Greg, Chris, etc])中,并对其使用random.choice()函数。

您可以使用Greg["HairLenght"]访问字典中的条目,它将返回"Short"

这将大大缩短代码,而且您也不需要将变量作为="unassigned"引入

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40038231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档