Why is a function calling itself?

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 doing.

code

Author: Maniero, 2017-03-16

1 answers

The Last Line is an isolated code and sends clinic() to execute, without it nothing would be executed. Functions are performed only when they are called.

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

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

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

I taught a person the best this in difference between two codes .

Escape from this course you are teaching wrong:)

 6
Author: Maniero, 2017-04-13 12:59:32