Path: csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '"this': 0.03; 'else:': 0.03; '"""': 0.07; 'c++,': 0.07; 'function,': 0.09; 'python': 0.11; 'def': 0.12; 'itself.': 0.14; 'times,': 0.14; '"yes":': 0.16; 'ends,': 0.16; 'handled.': 0.16; 'loop.': 0.16; 'program...': 0.16; 'strange,': 0.16; 'thread,': 0.16; 'true:': 0.16; 'unlikely': 0.16; 'java,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'bit': 0.19; 'user.': 0.19; 'seems': 0.21; 'input': 0.22; 'programming': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; '"you': 0.24; 'own.': 0.24; 'why.': 0.24; '(or': 0.24; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'fixed': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; 'usually': 0.31; 'end,': 0.31; 'anyone': 0.31; 'probably': 0.32; 'could': 0.34; 'common': 0.35; 'something': 0.35; 'case,': 0.35; 'point.': 0.35; 'but': 0.35; 'version': 0.36; 'done,': 0.36; "i'll": 0.36; 'similar': 0.36; 'wrong': 0.37; 'turn': 0.37; 'starting': 0.37; 'needed': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'explain': 0.39; 'does': 0.39; 'environment.': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'space': 0.40; 'most': 0.60; 'tell': 0.60; "you're": 0.61; "you've": 0.63; 'show': 0.63; 'pick': 0.64; 'occur': 0.65; 'within': 0.65; 'here': 0.66; 'received:74.208': 0.68; 'video,': 0.68; 'limit': 0.70; 'press': 0.70; 'ending': 0.78; 'complaint': 0.84; 'recursion,': 0.84; 'tricky': 0.84; 'why?': 0.91; 'differences': 0.93 Date: Mon, 22 Dec 2014 15:35:25 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Program calling unwanted functions References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:Q618IdGdHhPuHbYpg4j5hajdZmbLtQb4mHSj1uiIQqu uvniXZTQQT2VZHcErgAWIbquu3B+Y8NAaSmtVzEzBIbXR80oo4 FAgwwWkvE45Fm5IfEfduh8IkEp6QOkVqi9UjoIKJDqno1XcFx4 wSNy7ICCOXBkEtuz9JoGfX9YCByO+yQjpFL+aQ5VB42gaXkVQv EIs0pAFhvtHRLAhwRL18lNmNWKNUfIAqlf6Hp5g03i37p1gqdS sG+fAldQl2dDwxv+DzafKAwLcZJyIVgaQIdEjBlXtHNSQrw7bW 17wU2Urk8uQYta5ZH35b0oNMsVezyO68SEa9eEuwnkXk+/G9g= = X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 115 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1419280539 news.xs4all.nl 2958 [2001:888:2000:d::a6]:45959 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82805 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