Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the point of the 2 clinic() at the end?
Python
def clinic():
    print "You've just entered the clinic!"
    print "Do you take the door on the left or the right?"
    answer = 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()


What I have tried:

I have tried stack overflow but nobody knew I guess.
Posted
Updated 17-Jul-22 20:32pm
v2
Comments
[no name] 18-Jul-22 3:19am    
Good

Quote:
What is the point of the 2 clinic() at the end?

First: run your code with the last 'clinic' then run the without the last clinic.
Answer should be obvious.
Then do the same with other 'clinic' abd run the code with an unwanted input
Answer should be obvious.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
CPallini 18-Jul-22 6:06am    
5.
Patrice T 18-Jul-22 6:19am    
Thank you
Python
#-> 'clinic' function definition starts
def clinic():
    print "You've just entered the clinic!"
    print "Do you take the door on the left or the right?"
    answer = 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() # recursive call to 'clinic' function
#<- 'clinic' function definition ends

#-> program starts
clinic()
#<- program ends

As shown by the comments, the 'second clinic()' at the end, is actually the program call to the function, while the 'first clinic()' is just a recursive call inside thefunction itself.

Note: as Dave suggested, there is no point in using recursion, in such a program.
A simple iterative algorithm fits better. Try, for instance:
Python
while True:
  print("You've just entered the clinic!")
  print("Do you take the door on the left or the right?")
  answer = 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!")
    break # exit the while loop
  elif answer == "right" or answer == "r":
    print("Of course this is the Argument Room, I've told you that already!")
    break # exit the while loop
  else:
    print("You didn't pick left or right! Try again.")
    # the while loop continues...
 
Share this answer
 
v2
Comments
Patrice T 18-Jul-22 5:04am    
+5
CPallini 18-Jul-22 6:06am    
Thank you.
Sigh. Where did you get this student level code?

This is a piece of code that defines a single function, "clinic". It's basically defining the entire program in this one function, and does a horrible job of it.

The call at the end of the function just calls the function again if you didn't enter what the code is expecting at a prompt. This is a garbage way to rerun the code because it can overrun the end of the stack and crash the app.

The call at the very end of your post just starts the app by calling the function "clinic".
 
Share this answer
 
Comments
CPallini 18-Jul-22 2:06am    
5.
Naveen Krishna1 18-Jul-22 9:09am    
ok thanks
i got this from codecademy
Dave Kreskowiak 18-Jul-22 9:52am    
You got that code from the forums at codeacademy. That's a really bad place to get example code from.
Richard Deeming 19-Jul-22 4:22am    
"it can overrun the end of the stack and crash the app"

To be fair, he did say he'd tried stack overflow! 🤣
Dave Kreskowiak 19-Jul-22 12:04pm    
:laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900