函数错误中的Python函数?
问题描述:
我写了这个作为一个例子:函数错误中的Python函数?
def function():
choice = raw_input("> ")
if choice == "attack":
print "Killed the monster."
elif choice == "run":
print "You run but the monster follows you. Activate jetpack? (y/n)"
def function_in_function():
choice = raw_input("> ")
if choice == "y":
print "You fly to safety."
elif choice == "n":
print "You get stomped."
else:
print "That's not an option."
function_in_function()
else:
print "That's not an option."
function()
我打开了它的电源外壳,但一无所获?
答
我想这是你想要的功能和格式。我不建议在这里使用函数内部的函数,因为它不是必需的,但它仍然可以如图所示完成。记住缩进。在这里阅读http://www.python-course.eu/python3_blocks.php。另外,在分配它之前,您不能调用函数。
def function():
choice = raw_input("> ")
if choice == "attack":
print "Killed the monster."
elif choice == "run":
print "You run but the monster follows you. Activate jetpack? (y/n)"
def function_in_function():
choice = raw_input("> ")
if choice == "y":
print "You fly to safety."
elif choice == "n":
print "You get stomped."
else:
print "That's not an option."
function_in_function()
function_in_function()
else:
print "That's not an option."
function()
function()
编辑:根据评论。
顶行下方的所有内容至少需要缩进4个空格。 – Andrew
缩进是错误的。 –
如果这被保存为一个.py文件,并且你执行该文件,则不会发生任何事情,因为你所做的只是定义一个函数,但从不执行它。 – syntonym