Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82801 > unrolled thread

Program calling unwanted functions

Started byLuke Tomaneng <luketomaneng@gmail.com>
First post2014-12-22 11:55 -0800
Last post2014-12-22 19:12 -0500
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Program calling unwanted functions Luke Tomaneng <luketomaneng@gmail.com> - 2014-12-22 11:55 -0800
    Re: Program calling unwanted functions John Gordon <gordon@panix.com> - 2014-12-22 20:09 +0000
    Re: Program calling unwanted functions Dave Angel <davea@davea.name> - 2014-12-22 15:35 -0500
    Re: Program calling unwanted functions Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-22 16:48 -0700
    Re: Program calling unwanted functions Dave Angel <davea@davea.name> - 2014-12-22 18:57 -0500
      Re: Program calling unwanted functions sohcahtoa82@gmail.com - 2014-12-22 16:04 -0800
        Re: Program calling unwanted functions Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-22 17:12 -0700
        Re: Program calling unwanted functions Dave Angel <davea@davea.name> - 2014-12-22 19:12 -0500

#82801 — Program calling unwanted functions

FromLuke Tomaneng <luketomaneng@gmail.com>
Date2014-12-22 11:55 -0800
SubjectProgram calling unwanted functions
Message-ID<b37a93ec-c90b-4375-a264-e96c3128871b@googlegroups.com>
Hello to all those in this forum,
    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?

[toc] | [next] | [standalone]


#82803

FromJohn Gordon <gordon@panix.com>
Date2014-12-22 20:09 +0000
Message-ID<m79tqh$jdr$1@reader1.panix.com>
In reply to#82801
In <b37a93ec-c90b-4375-a264-e96c3128871b@googlegroups.com> Luke Tomaneng <luketomaneng@gmail.com> writes:

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

The cameraman function restarts itself because ... that's what you told it
to do.  As far as I can see, every possible logic branch ends up with a
call to cameraman().

The kid function doesn't call instructions, but the last line of your
script does call it, unconditionally.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [next] | [standalone]


#82805

FromDave Angel <davea@davea.name>
Date2014-12-22 15:35 -0500
Message-ID<mailman.17136.1419280539.18130.python-list@python.org>
In reply to#82801
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

[toc] | [prev] | [next] | [standalone]


#82817

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-12-22 16:48 -0700
Message-ID<mailman.17144.1419292133.18130.python-list@python.org>
In reply to#82801

[Multipart message — attachments visible in raw view] — view raw

On Dec 22, 2014 2:37 PM, "Dave Angel" <davea@davea.name> wrote:
>
> 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.

Point of interest, Python (or at least CPython) doesn't optimize tail-call
recursion, so in fact this sort of unbounded recursion (a "stack leak", if
you will) is equally bad no matter where it appears in the function.

[toc] | [prev] | [next] | [standalone]


#82818

FromDave Angel <davea@davea.name>
Date2014-12-22 18:57 -0500
Message-ID<mailman.17145.1419292640.18130.python-list@python.org>
In reply to#82801
On 12/22/2014 06:48 PM, Ian Kelly wrote:
> On Dec 22, 2014 2:37 PM, "Dave Angel" <davea@davea.name> wrote:
>>
>> 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.
>
> Point of interest, Python (or at least CPython) doesn't optimize tail-call
> recursion, so in fact this sort of unbounded recursion (a "stack leak", if
> you will) is equally bad no matter where it appears in the function.
>

Which is exactly why I mentioned the recursion limit of about 1000.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#82819

Fromsohcahtoa82@gmail.com
Date2014-12-22 16:04 -0800
Message-ID<02c018f7-d871-4b3c-abd4-0fca1fbe12c1@googlegroups.com>
In reply to#82818
On Monday, December 22, 2014 3:57:31 PM UTC-8, Dave Angel wrote:
> On 12/22/2014 06:48 PM, Ian Kelly wrote:
> > On Dec 22, 2014 2:37 PM, "Dave Angel" <davea@davea.name> wrote:
> >>
> >> 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.
> >
> > Point of interest, Python (or at least CPython) doesn't optimize tail-call
> > recursion, so in fact this sort of unbounded recursion (a "stack leak", if
> > you will) is equally bad no matter where it appears in the function.
> >
> 
> Which is exactly why I mentioned the recursion limit of about 1000.
> 
> 
> -- 
> DaveA

Python 2.7.3 (default, Dec  4, 2012, 17:28:13)
[GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def test(t):
...     print(t)
...     test(t+1)
...
>>> test(1)
1
2
3
4
<snip>
998
999
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in test
  File "<stdin>", line 3, in test
  File "<stdin>", line 3, in test
<snip>
  File "<stdin>", line 3, in test
  File "<stdin>", line 3, in test
RuntimeError: maximum recursion depth exceeded

Huh...there actually is a limit of about 1,000.  I'm assuming this is hard-coded?  I did a similar test with Java a while back and was getting different results every time.

[toc] | [prev] | [next] | [standalone]


#82820

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-12-22 17:12 -0700
Message-ID<mailman.17146.1419293543.18130.python-list@python.org>
In reply to#82819

[Multipart message — attachments visible in raw view] — view raw

On Dec 22, 2014 6:06 PM, <sohcahtoa82@gmail.com> wrote:
>
> Huh...there actually is a limit of about 1,000.  I'm assuming this is
hard-coded?  I did a similar test with Java a while back and was getting
different results every time.

The default is 1000 but it can be configured with sys.setrecursionlimit. I
think Java only uses a memory limit, not a frame limit.

[toc] | [prev] | [next] | [standalone]


#82877

FromDave Angel <davea@davea.name>
Date2014-12-22 19:12 -0500
Message-ID<mailman.17177.1419410427.18130.python-list@python.org>
In reply to#82819
On 12/22/2014 07:04 PM, sohcahtoa82@gmail.com wrote:
>>>> def test(t):
> ...     print(t)
> ...     test(t+1)
> ...
>>>> test(1)
> 1
> 2
> 3
> 4
> <snip>
> 998
> 999
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "<stdin>", line 3, in test
>    File "<stdin>", line 3, in test
>    File "<stdin>", line 3, in test
> <snip>
>    File "<stdin>", line 3, in test
>    File "<stdin>", line 3, in test
> RuntimeError: maximum recursion depth exceeded
>
> Huh...there actually is a limit of about 1,000.  I'm assuming this is hard-coded?  I did a similar test with Java a while back and was getting different results every time.
>

https://docs.python.org/3.4/library/sys.html#sys.getrecursionlimit


-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web