On 12/22/2014 02:55 PM, Luke Tomaneng wrote:
Hello to all those in this forum,

Hello. When starting a thread, please tell us your environment. In this case, it would be the python version and OS.

     My code seems to have a mind of its own. I have been writing a program to reenact 
the "Twenny Wun" Vine video, and it seems to be activating  functions without 
me calling them. Here is the script:

def kid():
     print "Cameraman: You stupid."
     kid1 = raw_input("Kid: ")
     if kid1.lower() == "no im not" or kid1.lower() == "no i'm not.":
         print "Cameraman: What's nine plus ten?"
         kid2 = raw_input("Kid: ")
         if kid2.lower() == "twenny wun" or kid2.lower() == "twenty-one" or kid2.lower() == "twenty 
one" or kid2 == "21" or kid2.lower() == "twenny one":
             print """Cameraman: You stupid.
Ending program...
"""
         else:
             print "That is not the right quote."
             kid()
     else:
         print "That is not the right quote."
         kid()
def cameraman():
     cameraman1 = raw_input("Cameraman: ")
     if cameraman1.lower() == "you stupid":
         print "Kid: No I'm not."
         cameraman2 = raw_input("Cameraman: ")
         if cameraman2.lower() == "whats 9 + 10" or cameraman2.lower() == "whats 
nine plus ten":
             print "Kid: Twenny wun"
             cameraman3 = raw_input("Cameraman: ")
             if cameraman3.lower() == "you stupid":
                 print "Ending program..."
                 time.sleep(2)
             else:
                 print "That is not the right quote."
             cameraman()
         else:
             print "That is not the right quote."
             cameraman()
     else:
         print "That is not the right quote."
         cameraman()
def perspective():
     perspective_request = raw_input("Do you want to be the cameraman or the kid? 
(type the one you want): ")
     if perspective_request == "cameraman":
         cameraman()
     if perspective_request == "kid":
         kid()
     else:
         print "Invalid input."
         perspective()
def instructions():
     instructions_request = raw_input("Do you want instructions? (type 'yes' or 'no' 
without the quotes): ")
     if instructions_request == "no":
         perspective()
     if instructions_request == "yes":
         print "This is a reenactment of the 'Twenny Wun' Vine. You can type in the 
empty space to the right of each ':,' then press [return]. Don't use punctuation."
         perspective()
     else:
         print "Invalid input."
         instructions()
instructions()

The "cameraman" function restarts itself when it ends, and the "kid" function calls 
"instructions()." Does anyone know why?


The kid() function doesn't call instructions, you do.  On that last line.

However all of your functions are recursive, and do not do what you expect them to. You're using the function call as though it were a goto, and that's not correct in Python (or in C, or C++, Java, or most any other common language).

Recursion in your case is where you call a function from within the same function. It could also occur as mutual recursion, where function A calls B, and B in turn calls A.

I'll pick on one function first, called instructions(). If the user types something invalid, you print "Invalid input." and call the function again. In this case, because the call is at the end, no harm is usually done, but it would be tricky to explain why. If the user happened to type the wrong input 1000 times, you'd hit a recursion limit and crash, but that's unlikely unless you have a very stubborn user.

The right way to express what is needed is to use a while loop.

def instructions():
    while True:
instructions_request = raw_input("Do you want instructions? (type 'yes' or 'no' without the quotes): ")
        if instructions_request == "no":
            perspective()
            return
        if instructions_request == "yes":
print "This is a reenactment of the 'Twenny Wun' Vine. You can type in the empty space to the right of each ':,' then press [return]. Don't use punctuation."
            perspective()
            return
        else:
            print "Invalid input."


Most of the other functions can be fixed in a similar way. Your complaint about the cameraman function is a bit strange, since the function never ends. It always calls itself. if you've got a place where it should, and that place is not physically on the last line of the function, you probably need to say "return" at that point.

It might help if you tell us what programming language you used before, so we can show the differences in the way these things might be handled.
--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to