Why is a function calling itself?

Asked

Viewed 1,292 times

3

def clinic():
    print "Voce acabou de entrar na clinica!"
    print "Voce entra pela porta a esquerda (left) ou a direita (right)?"
    answer = raw_input("Digite left (esquerda) ou right (direita) e pressione 'Enter'.").lower()
    if answer == "left" or answer == "l":
        print "Esta e a sala de Abuso Verbal, seu monte de caca de papagaio!"
    elif answer == "right" or answer == "r":
        print "E claro que esta e a Sala das Discussoes. Eu ja disse isso!"
    else:
        print "Voce nao escolheu esquerda ou direita. Tente de novo."
        clinic()
clinic()

What is the need for these two clinic() in the end? Only 1 of them would apparently work the code.

It wasn’t me who wrote the code, it was from a course I’m taking.

código

  • Can you confirm the code’s indentation? I tried to fix it, but I need you to confirm that I did it right (without the correct indentation, we have no way to evaluate it correctly).

  • If the above code is correctly indented the last Clinic() calls the method for the first time.

  • 1

    In time: use Python3 instead of python 2.7 - then you can tell your story with accents, without having to worry about syntax errors.

1 answer

6


The last line is an isolated code and has the clinic(), without it nothing would run. Functions are executed only when they are called.

The previous one is within the function clinic(), that is, she calls herself. That is called recursion, but it was probably accidental. This can cause problems like the stack overflow. Prefer to make a code with a repeat loop, probably a while to repeat when necessary.

Note that this recursive call will only occur if the previous conditions of if were false.

So this call is not necessary if the code is done as it should be done.

I taught one person to better it in Difference between two codes.

Run away from this course you are teaching wrong :)

  • This course is codeacademy, it looks so good. I will read your other topic to clear up here

  • 2

    That’s why I don’t like to recommend course, almost everyone is wrong.

  • Just for the record, thanks to his explanation I understood what he wanted to do in the code, if it falls into the false condition he re-enters the code again, then in that case it is necessary to even call the Clinic(), I am sure?

  • 3

    I believe that, by default, the recursion depth in Python is 1000. In this case it would be difficult to achieve, but it sure is not good to already create bad addictions.

  • 5

    In the form made, yes, but this form is problematic and should not be used, never, unless in case where recursion makes sense. @Andersoncarloswoss no doubt, but I don’t teach anyone to do what they shouldn’t, because I believe in right, not what works, what works can stop working, right only becomes wrong by changing the requirement, and if it was right even the change is not traumatic. In fact, it’s the little guy who twists the cucumber :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.