The Big If: is there any differences b/w these 2 code blocks?


#1

Is there any difference b/w below code blocks? I run 1st block, it throws "Oops, try again. The following exception was raised when calling the_flying_circus(): RuntimeError: maximum recursion depth exceeded while calling a Python object" while 2nd does not.

>  # Make sure that the_flying_circus() returns True
>     def the_flying_circus():
>         decision = raw_input("Do you want to go to the flying circus?")
>         if decision == "Yes" or decision == 'Y':    
>             print "OK, we'll go!"
>         elif decision == "No" or decision == 'N':
>             print "OK, see you next time!"
>         else:
>             print "Invalid decision. Please input Yes or No"
>             the_flying_circus()

>     the_flying_circus()

and

> def clinic():
>     print "You've just entered the clinic!"
>     print "Do you take the door on the left or the right?"
>     answer = raw_input("Type left or right and hit 'Enter'.").lower()
>     if answer == "left" or answer == "l":
>         print "This is the Verbal Abuse Room, you heap of parrot droppings!"
>     elif answer == "right" or answer == "r":
>         print "Of course this is the Argument Room, I've told you that already!"
>     else:
>         print "You didn't pick left or right! Try again."
>         clinic()

> clinic()

#2

Could anyone help me please? :relaxed:


#3

Your code is correct.

Problem is that after you submit your code codecademy has to check if your code is correct.

They have to try to execute your code, your script asks them for input, they are not giving any so we are now inside else block and we are starting again. Function call -> lack of input -> else block -> function call -> lack of input -> else block -> function cal... ok, you get it, right? That is why your code reaches the limit of depth.

How to fix this? Delete recursive call from the else block:

def the_flying_circus():
    decision = raw_input("Do you want to go to the flying circus?")
    if decision == "Yes" or decision == 'Y':
        print "OK, we'll go!"
    elif decision == "No" or decision == 'N':
        print "OK, see you next time!"
    else:
        print "Invalid decision. Please run script again."
        return True

the_flying_circus()

#4

Hi factoradic,

I got it. Thanks for your answer :relaxed:


#5

You're very welcome :smiley:


#6

Hi factoradic,

For case I still want to ask user re-enter his decision, I find that we can do it like this :relaxed: :

def the_flying_circus():
    decision = raw_input("Do you want to go to the flying circus?")
    if decision == "Yes" or decision == 'Y':
        print "OK, we'll go!"
    elif decision == "No" or decision == 'N':
        print "OK, see you next time!"
    else:
        print "Invalid decision. Please input Yes or No."
        return the_flying_circus()

the_flying_circus()