Keretto Keretto - 3 months ago 12
Python Question

Python Function in Function error?

I wrote this as an example:

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()


I opened it on power shell but nothing came up?

Answer

I guess this is your desired functionality and formatting. I wouldn't recommend using function inside function here, since it's not necessary, but it still can be done as shown. Remember about indentation. Read here http://www.python-course.eu/python3_blocks.php. Also, you can't call a function before assigning it.

def function():
    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()
    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)"
        function_in_function()
    else:
        print "That's not an option."
        function()


function()

Edit: according to the comment.

Comments