[Tutor] why different result from two similar ways

Frank Pontius yahufpontius at yahoo.com
Tue Oct 30 02:36:02 CET 2012


Hello,
I have code that works.  Then tried to move some of it into function
IncrementAndRebuildInput, then result changes, I no longer have same result
as when code in function was inline - why?

(Function version way below)

Inline version:     (this correctly adds 1 to all numbers in text string,
and prints it out with incremented #s):

def IsNum(string):
#    print "IsNum string", string
    for char in string:             #checks string groupings to be all nums
        if not char.isdigit():
#            print "false"
            return False
#    print "true"
    return True


def main():
    text = raw_input("Type something: ")
    print
    if text:
        print text
    else:
        text = "I got 432 when I counted, but Jim got 433 which is a lot for
only 6 cats, or were there 12 cats?"
        print text                              #string input
        
    SplitText = text.split()                #makes a list from string input
#               print SplitText
#               print "Did I print LIST?"
    for index, element in enumerate(SplitText):
        if IsNum(element):                  #looks at every list element,
checks for #
            num = int(element) + 1          #if #, increments it
#               print num
#               print "Bkpt8"
            SplitText[index] = str(num)
        else:
            pass
#                print "bkpt9"

 #   NewString = " ".join(SplitText)
    print "bkpt10"
    print
    print SplitText
    print
    print " ".join(SplitText)
    print
    print "END"
main()

OUTPUT:::
>>>
>>> 
Type something: 

I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or
were there 12 cats?
bkpt10

['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were',
'there', '13', 'cats?']

I got 433 when I counted, but Jim got 434 which is a lot for only 7 cats, or
were there 13 cats?

END
>>>


Function version way below:		(This version does not produce the
same output (w/numbers incremented by 1)

def IsNum(string):
#    print "IsNum string", string
    for char in string:             #checks string groupings to be all nums
        if not char.isdigit():
#            print "false"
            return False
#    print "true"
    return True



def IncrementAndRebuildInput(text):
    newtext = text.split()                #makes a list from string input
    print newtext
#               print "Did I print LIST?"
    for index, element in enumerate(newtext):
        if IsNum(element):                  #looks at every list element,
checks for #
            num = int(element) + 1          #if #, increments it
            print num
#               print "Bkpt8"
            newtext[index] = str(num)
            print newtext
            print "NOWHERE"
        else:
            pass
#                print "bkpt9"
    print newtext                 # contains new list w/#'s incremented by 1
    print "Point6"
    return newtext


def main():
    text = raw_input("Type something: ")
    print
    if text:
        print text
    else:
        text = "I got 432 when I counted, but Jim got 433 which is a lot for
only 6 cats, or were there 12 cats?"
        print text                              #string input

    IncrementAndRebuildInput(text)

#       print "bkpt10"
    print
    print text                        #  **********  Placing previous inline
code into function changes result - what am I doing wrong?    **********
    print "Point7"
    print "".join(text)
    print
    print "END"
main()

OUTPUT:::
>>>
>>> 
Type something: 

I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or
were there 12 cats?
['I', 'got', '432', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '433',
'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were',
'there', '12', 'cats?']
433
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '433',
'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were',
'there', '12', 'cats?']
NOWHERE
434
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were',
'there', '12', 'cats?']
NOWHERE
7
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were',
'there', '12', 'cats?']
NOWHERE
13
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were',
'there', '13', 'cats?']
NOWHERE
['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434',
'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were',
'there', '13', 'cats?']
Point6

I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or
were there 12 cats?
Point7
I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or
were there 12 cats?

END
>>> 
>>>




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121029/973c2441/attachment.html>


More information about the Tutor mailing list