Python 我的一个变量似乎不起作用,我';我找不到

Python 我的一个变量似乎不起作用,我';我找不到,python,python-2.7,variables,Python,Python 2.7,Variables,我正在学习python的艰难之路课程,我不得不用python编写自己的小故事。我确实做到了,我花了很多时间解决了它的许多问题,但有一个变量导致了我的许多错误 这是我的密码: def party(): print "You are at a friend's house, and there you find three men who disrespect you.\ \nDo you fight them, insult back, or do you invite them for a dr

我正在学习python的艰难之路课程,我不得不用python编写自己的小故事。我确实做到了,我花了很多时间解决了它的许多问题,但有一个变量导致了我的许多错误

这是我的密码:

def party():
print "You are at a friend's house, and there you find three men who disrespect you.\
\nDo you fight them, insult back, or do you invite them for a drink?"
party_answer = raw_input()
while (party_answer != "fight") and (party_answer != "insult") and (party_answer != "drink"):
    print "Type either 'fight', 'insult', or 'drink'"
    party_answer = raw_input()  

if party_answer == "fight":
    fight()
elif party_answer == "insult":
    insult()
elif party_answer == "drink":
    drink()


def fight():
    print """You decide to fight the men.
After a while, the men start pleading for mercy.
Do you hit them more?"""
fight_answer = raw_input()
while (fight_answer != "yes") and (fight_answer != "no"):
    print "Type either 'yes' or 'no'"
    fight_answer = raw_input()

if fight_answer == "yes":
    print """Your friend sees you fighting and asks you why
Do you blame them, or do you take responsibility and apologize?"""
    fight_answer_y = raw_input()
    while (fight_answer_y != "blame them") and (fight_answer_y != "apologize"):
        print "Type either 'blame them' or 'apologize'"
        fight_answer_y = raw_input()

    if fight_answer_y == "blame them":
        print """Your friend is angry at you fighting at all, and does not believe \
that they started the fight. He kicks you out of the party."""
        exit(1)
    elif fight_answer_y == "apologize":
        print "Your friend forgives you, and asks you to drink with him."
        exit(2)

elif fight_answer == "no":
    print """The men take advantage of you forgiveness and start hitting you.
Fortunately, your friend arrives at this moment. He see them hitting you, \
and then kicks them out of the party."""
    exit(2)

def insult():
print """You insult them, and they challenge you to a fight.
Do you fight them or tell your friend?"""
insult_answer = raw_input()
while (insult_answer != "fight") and (insult_answer != "tell friend"):
    print "Type either 'fight' or 'tell friend'"
    insult_answer = raw_input()

if insult_answer == "fight":
    fight()
elif insult_answer == "tell friend":
    print """You decide to tell your friend.
He says he is happy you told him, and invites you for a drink
The men get kicked out"""
    exit(2)

def drink():
print """You tell them that you will buy drinks for them,\
and they rudely ask you why you are doing this.
Do you insult them or give them a polite response?"""
drink_answer = raw_input()
while (drink_answer != "insult") and (drink_answer != "polite response"):
    print "Type either 'insult' or 'polite response'"
    drink_answer = raw_input()

if drink_answer == "insult":
    insult()
elif drink_answer == "polite response":
    print "You politely respond to them, and they sit down with you and apologize."
    exit(2)

def exit(x):
if x == 1:
    print "You lost. Better luck next time!"
    again = raw_input("Do you want to try again?: ")
    while (again != "yes") and (again != "no"):
        print "Type either 'yes' or 'no'"
        again = raw_input("Do you want to try again?: ")

    if again == "yes":
        party()
    elif again == "no":
        quit()

elif x == 2:
    print "You won. Good job!"
    again = raw_input("Do you want to try again?: ")
    while (again != "yes") and (again != "no"):
        print "Type either 'yes' or 'no'"
        again = raw_input("Do you want to try again?: ")

    if again == "yes":
        party()
    elif again == "no":
        quit() 

party()
我似乎在变量“fight\u answer\y”方面出错了 下面是我得到的错误:

  File "mygame1.py", line 38, in fight
    elif fight_answer_y == "apologize":
UnboundLocalError: local variable 'fight_answer_y' referenced before assignment
谁能帮我解释一下,因为我是Python新手。
提前感谢

您的缩进错误。据我所知,这似乎有效

def party():
    print "You are at a friend's house, and there you find three men who disrespect you.\
    \nDo you fight them, insult back, or do you invite them for a drink?"
    party_answer = raw_input()
    while (party_answer != "fight") and (party_answer != "insult") and (party_answer != "drink"):
        print "Type either 'fight', 'insult', or 'drink'"
        party_answer = raw_input()
    if party_answer == "fight":
        fight()
    elif party_answer == "insult":
        insult()
    elif party_answer == "drink":
        drink()


def fight():
    print """You decide to fight the men.
After a while, the men start pleading for mercy.
Do you hit them more?"""
    fight_answer = raw_input()
    while (fight_answer != "yes") and (fight_answer != "no"):
        print "Type either 'yes' or 'no'"
        fight_answer = raw_input()

    if fight_answer == "yes":
        print """Your friend sees you fighting and asks you why
Do you blame them, or do you take responsibility and apologize?"""
        fight_answer_y = raw_input()
        while (fight_answer_y != "blame them") and (fight_answer_y != "apologize"):
            print "Type either 'blame them' or 'apologize'"
            fight_answer_y = raw_input()

        if fight_answer_y == "blame them":
            print """Your friend is angry at you fighting at all, and does not believe \
that they started the fight. He kicks you out of the party."""
            exit(1)
        elif fight_answer_y == "apologize":
            print "Your friend forgives you, and asks you to drink with him."
            exit(2)

    elif fight_answer == "no":
        print """The men take advantage of you forgiveness and start hitting you.
Fortunately, your friend arrives at this moment. He see them hitting you, \
and then kicks them out of the party."""
        exit(2)

def insult():
    print """You insult them, and they challenge you to a fight.
    Do you fight them or tell your friend?"""
    insult_answer = raw_input()
    while (insult_answer != "fight") and (insult_answer != "tell friend"):
        print "Type either 'fight' or 'tell friend'"
        insult_answer = raw_input()

    if insult_answer == "fight":
        fight()
    elif insult_answer == "tell friend":
        print """You decide to tell your friend.
He says he is happy you told him, and invites you for a drink
The men get kicked out"""
        exit(2)

def drink():
    print """You tell them that you will buy drinks for them,\
and they rudely ask you why you are doing this.
Do you insult them or give them a polite response?"""
    drink_answer = raw_input()
    while (drink_answer != "insult") and (drink_answer != "polite response"):
        print "Type either 'insult' or 'polite response'"
        drink_answer = raw_input()

    if drink_answer == "insult":
        insult()
    elif drink_answer == "polite response":
        print "You politely respond to them, and they sit down with you and apologize."
        exit(2)

def exit(x):
    if x == 1:
        print "You lost. Better luck next time!"
        again = raw_input("Do you want to try again?: ")
        while (again != "yes") and (again != "no"):
            print "Type either 'yes' or 'no'"
            again = raw_input("Do you want to try again?: ")

        if again == "yes":
            party()
        elif again == "no":
            quit()

    elif x == 2:
        print "You won. Good job!"
        again = raw_input("Do you want to try again?: ")
        while (again != "yes") and (again != "no"):
            print "Type either 'yes' or 'no'"
            again = raw_input("Do you want to try again?: ")

        if again == "yes":
            party()
        elif again == "no":
            quit()

party()

你的缩进是错的。据我所知,这似乎有效

def party():
    print "You are at a friend's house, and there you find three men who disrespect you.\
    \nDo you fight them, insult back, or do you invite them for a drink?"
    party_answer = raw_input()
    while (party_answer != "fight") and (party_answer != "insult") and (party_answer != "drink"):
        print "Type either 'fight', 'insult', or 'drink'"
        party_answer = raw_input()
    if party_answer == "fight":
        fight()
    elif party_answer == "insult":
        insult()
    elif party_answer == "drink":
        drink()


def fight():
    print """You decide to fight the men.
After a while, the men start pleading for mercy.
Do you hit them more?"""
    fight_answer = raw_input()
    while (fight_answer != "yes") and (fight_answer != "no"):
        print "Type either 'yes' or 'no'"
        fight_answer = raw_input()

    if fight_answer == "yes":
        print """Your friend sees you fighting and asks you why
Do you blame them, or do you take responsibility and apologize?"""
        fight_answer_y = raw_input()
        while (fight_answer_y != "blame them") and (fight_answer_y != "apologize"):
            print "Type either 'blame them' or 'apologize'"
            fight_answer_y = raw_input()

        if fight_answer_y == "blame them":
            print """Your friend is angry at you fighting at all, and does not believe \
that they started the fight. He kicks you out of the party."""
            exit(1)
        elif fight_answer_y == "apologize":
            print "Your friend forgives you, and asks you to drink with him."
            exit(2)

    elif fight_answer == "no":
        print """The men take advantage of you forgiveness and start hitting you.
Fortunately, your friend arrives at this moment. He see them hitting you, \
and then kicks them out of the party."""
        exit(2)

def insult():
    print """You insult them, and they challenge you to a fight.
    Do you fight them or tell your friend?"""
    insult_answer = raw_input()
    while (insult_answer != "fight") and (insult_answer != "tell friend"):
        print "Type either 'fight' or 'tell friend'"
        insult_answer = raw_input()

    if insult_answer == "fight":
        fight()
    elif insult_answer == "tell friend":
        print """You decide to tell your friend.
He says he is happy you told him, and invites you for a drink
The men get kicked out"""
        exit(2)

def drink():
    print """You tell them that you will buy drinks for them,\
and they rudely ask you why you are doing this.
Do you insult them or give them a polite response?"""
    drink_answer = raw_input()
    while (drink_answer != "insult") and (drink_answer != "polite response"):
        print "Type either 'insult' or 'polite response'"
        drink_answer = raw_input()

    if drink_answer == "insult":
        insult()
    elif drink_answer == "polite response":
        print "You politely respond to them, and they sit down with you and apologize."
        exit(2)

def exit(x):
    if x == 1:
        print "You lost. Better luck next time!"
        again = raw_input("Do you want to try again?: ")
        while (again != "yes") and (again != "no"):
            print "Type either 'yes' or 'no'"
            again = raw_input("Do you want to try again?: ")

        if again == "yes":
            party()
        elif again == "no":
            quit()

    elif x == 2:
        print "You won. Good job!"
        again = raw_input("Do you want to try again?: ")
        while (again != "yes") and (again != "no"):
            print "Type either 'yes' or 'no'"
            again = raw_input("Do you want to try again?: ")

        if again == "yes":
            party()
        elif again == "no":
            quit()

party()

简短回答:始终将编辑器配置为将选项卡扩展到四个空格,每个,不要让它将选项卡显示为四个空格,因为它隐藏了混合选项卡和空格的问题

说明:在中,问题是
elif
和相关块缩进太浅,附加到外部
if
而不是内部
if
上。问题是因为你混合了空格和制表符,
if-fight\u-answer\u y==“责备他们”:
缩进了两个制表符,而
elif-fight\u-answer\u y==“道歉”:
缩进了八个空格。您可能使用的编辑器将选项卡显示为四个空格(堆栈溢出也会这样做),因此它们以相同的对齐方式显示,但Python 2始终将选项卡视为等效于八个空格,因此代码如下所示:

if fight_answer == "yes":
    print """Your friend sees you fighting and asks you why
Do you blame them, or do you take responsibility and apologize?"""
    fight_answer_y = raw_input()
    while (fight_answer_y != "blame them") and (fight_answer_y != "apologize"):
        print "Type either 'blame them' or 'apologize'"
        fight_answer_y = raw_input()

    if fight_answer_y == "blame them":
        print """Your friend is angry at you fighting at all, and does not believe \
that they started the fight. He kicks you out of the party."""
        exit(1)
    elif fight_answer_y == "apologize": # <-- ***LOOKS LIKE ATTACHED TO if fight_answer_y == "blame them":***
        print "Your friend forgives you, and asks you to drink with him."
        exit(2)

elif fight_answer == "no":
    print """The men take advantage of you forgiveness and start hitting you.
如果回答=“是”:
打印“”,你的朋友看到你打架,问你为什么
你是责备他们,还是承担责任并道歉?”
战斗\u回答\u y=原始输入()
而(反抗,回答,责备他们)和(反抗,回答,道歉):
打印“键入‘责备他们’或‘道歉’”
战斗\u回答\u y=原始输入()
如果打架,回答“责怪他们”:
打印“”“你的朋友对你打架很生气,根本不相信\
他们开始打架,他把你踢出了派对
出口(1)

elif fight_answer_y==“道歉”:#简短回答:始终将编辑器配置为将选项卡扩展到四个空格,每个,不要只将选项卡显示为四个空格,因为它隐藏了混合选项卡和空格的问题

说明:在中,问题是
elif
和相关块缩进太浅,附加到外部
if
而不是内部
if
上。问题是因为你混合了空格和制表符,
if-fight\u-answer\u y==“责备他们”:
缩进了两个制表符,而
elif-fight\u-answer\u y==“道歉”:
缩进了八个空格。您可能使用的编辑器将选项卡显示为四个空格(堆栈溢出也会这样做),因此它们以相同的对齐方式显示,但Python 2始终将选项卡视为等效于八个空格,因此代码如下所示:

if fight_answer == "yes":
    print """Your friend sees you fighting and asks you why
Do you blame them, or do you take responsibility and apologize?"""
    fight_answer_y = raw_input()
    while (fight_answer_y != "blame them") and (fight_answer_y != "apologize"):
        print "Type either 'blame them' or 'apologize'"
        fight_answer_y = raw_input()

    if fight_answer_y == "blame them":
        print """Your friend is angry at you fighting at all, and does not believe \
that they started the fight. He kicks you out of the party."""
        exit(1)
    elif fight_answer_y == "apologize": # <-- ***LOOKS LIKE ATTACHED TO if fight_answer_y == "blame them":***
        print "Your friend forgives you, and asks you to drink with him."
        exit(2)

elif fight_answer == "no":
    print """The men take advantage of you forgiveness and start hitting you.
如果回答=“是”:
打印“”,你的朋友看到你打架,问你为什么
你是责备他们,还是承担责任并道歉?”
战斗\u回答\u y=原始输入()
而(反抗,回答,责备他们)和(反抗,回答,道歉):
打印“键入‘责备他们’或‘道歉’”
战斗\u回答\u y=原始输入()
如果打架,回答“责怪他们”:
打印“”“你的朋友对你打架很生气,根本不相信\
他们开始打架,他把你踢出了派对
出口(1)

elif fight_answer_y==“道歉”:#你的缩进在几个地方看起来是错误的;请把它修好,以符合你的实际情况。
fight\u response\u y
的用法看起来是正确的,但是如果缩进有一点偏离,它很容易打破你描述的方式;请把它修好,以符合你的实际情况。
fight\u response\u y
的用法看起来是正确的,但是如果缩进有点偏离,它很容易打破您描述的方式。下面是我的代码:谢谢!你能告诉我我出错的确切部分吗?因为我花了几个小时查看代码,没有遇到任何缩进错误。下面是我的代码:谢谢!你能告诉我我出错的确切部分吗?因为我花了几个小时查看代码,没有遇到任何缩进错误。下面是我的代码: