我正在努力学习python课程,我不得不使用python创建自己的小故事。我确实做到了,我花了很多时间来解决它的许多问题,但是有一个变量导致了很多错误。
这是我的代码:
$('#mySpinStrings', expiryControlsW).spinstrings({
options: ['day', 'week', 'month'],
value: 'week'
});
我似乎在变量“fight_answer_y”方面遇到错误 这是我得到的错误:
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()
任何人都可以帮助解释这个,因为我是Python的新手。 提前致谢
答案 0 :(得分:0)
你的缩进是错误的。就我所知,这似乎有效。
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()
答案 1 :(得分:0)
简短回答:始终将编辑器配置为将标签展开到四个空格,按PEP8,不要只显示标签为四个空格,因为它隐藏了混合标签和空格的问题。
说明:在your paste中,问题是elif
和关联的块缩进得太浅,附加到外部if
而不是内部if
& #39;应该依附于。问题是因为您正在混合空格和制表符,if fight_answer_y == "blame them":
缩进了两个制表符,而elif fight_answer_y == "apologize":
缩进了八个空格。您可能正在使用一个将选项卡显示为四个空格的编辑器(Stack Overflow也会这样做),因此它们以相同的对齐方式显示,但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.
感谢您的编辑器显示标签,因为四个空格缩进被解释为:
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": # <-- ***ACTUALLY ATTACHED TO if fight_answer == "yes":***
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.
感谢Python将相同的标签视为八个空格缩进。你运气不好,缩进似乎有效(大多数其他类似的失败意味着语法错误,或导致代码退出方法定义,因此失败是显而易见的)。在这种情况下,这意味着当fight_answer
不是"yes"
时,您尝试测试fight_answer_y == "apologize"
,但fight_answer_y
仅在fight_answer == "yes"
中定义case,意思是fight_answer_y
未定义。
如果您的编辑器配置为遵循PEP8(将标签展开到空格,使用四个空格缩进),则对齐问题就很明显了。如果您一直在使用Python 3,那么它对标签和空格的使用不一致的能力就会降低,而您的代码会报告TabError: inconsistent use of tabs and spaces in indentation
的问题。但事实上,你的代码看起来很正确,并且运行错误。再次: 总是使用PEP8空格规则来避免这样的问题。那里的每个编辑器(除了记事本)都可以配置为将标签扩展到具有四个空格缩进的空格;做到这一点。