Why is a function calling itself?

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.

I did not write the code, it was a course I'm doing.

    
asked by anonymous 16.03.2017 / 01:05

1 answer

6

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

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

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

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

I've taught you a better person at Difference between two codes .

Escape from this course that is teaching wrong:)

    
16.03.2017 / 01:19