Re: [Tutor] why different result from two similar ways

2012-11-06 Thread Steven D'Aprano

On 07/11/12 03:31, Prasad, Ramit wrote:

Steven D'Aprano wrote:



The isdigit method doesn't only work on a single character
at a time, it works on an entire string:

py>  "12345".isdigit()
True
py>  "12345a".isdigit()
False


I just want to point to the OP (Frank) that this only works for "digits"
i.e. integers. It will fail for other types of numbers.


That's why it's called "isdigit" not "isnumber" :)



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why different result from two similar ways

2012-11-06 Thread Prasad, Ramit
Steven D'Aprano wrote:
> On 30/10/12 12:36, Frank Pontius wrote:
> > 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?
> 
> Have you tried running it in isolation to see what it does?
> 
> When I try it, it works for me (apart from printing a lot of unnecessary
> intermediate results):
> 
> py> result = IncrementAndRebuildInput("abc def 123 xyz 456")
> ['abc', 'def', '123', 'xyz', '456']
> 124
> ['abc', 'def', '124', 'xyz', '456']
> NOWHERE
> 457
> ['abc', 'def', '124', 'xyz', '457']
> NOWHERE
> ['abc', 'def', '124', 'xyz', '457']
> Point6
> 
> 
> 
> Now check the returned result:
> 
> py> result
> ['abc', 'def', '124', 'xyz', '457']
> 
> So it certainly does increment the numbers in the string. The only
> bit it doesn't do is rebuild the string, but that takes just one
> minor change: instead of "return newstring" (by the way, that's false
> advertising -- newstring is not a string, it is a list), use:
> 
>  return ' '.join(newstring)
> 
> 
> You also use this function:
> 
> > 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
> 
> You don't need it! The isdigit method doesn't only work on a single character
> at a time, it works on an entire string:
> 
> py> "12345".isdigit()
> True
> py> "12345a".isdigit()
> False

I just want to point to the OP (Frank) that this only works for "digits" i.e. 
integers. It will fail for other types of numbers.

>>> '12.3'.isdigit()
False
>>> '12.3'.isalnum()
False

~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why different result from two similar ways

2012-11-05 Thread Steven D'Aprano

On 30/10/12 12:36, Frank Pontius wrote:

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?


Have you tried running it in isolation to see what it does?

When I try it, it works for me (apart from printing a lot of unnecessary
intermediate results):

py> result = IncrementAndRebuildInput("abc def 123 xyz 456")
['abc', 'def', '123', 'xyz', '456']
124
['abc', 'def', '124', 'xyz', '456']
NOWHERE
457
['abc', 'def', '124', 'xyz', '457']
NOWHERE
['abc', 'def', '124', 'xyz', '457']
Point6



Now check the returned result:

py> result
['abc', 'def', '124', 'xyz', '457']

So it certainly does increment the numbers in the string. The only
bit it doesn't do is rebuild the string, but that takes just one
minor change: instead of "return newstring" (by the way, that's false
advertising -- newstring is not a string, it is a list), use:

return ' '.join(newstring)


You also use this function:


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


You don't need it! The isdigit method doesn't only work on a single character
at a time, it works on an entire string:

py> "12345".isdigit()
True
py> "12345a".isdigit()
False




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why different result from two similar ways

2012-11-05 Thread Oscar Benjamin
On 30 October 2012 01:36, Frank Pontius  wrote:
> Hello,

Hi,

It would be good if you could remove unnecessary debug code before
posting since it makes it harder for others to read your actual code.
Also why is there an empty line between each two lines of code? I
think this makes it much harder to read the code.

>
> 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?

Because you're not using the result returned by the function.

>
> 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

You could always just return string.isdigit() instead of looping over
the characters in the string:

>>> s = '123'
>>> s.isdigit()
True
>>> s = '12r'
>>> s.isdigit()
False


>
>
>
> 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@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

Here the function returns the created list of strings.

>
>
> 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)

The function returns a list of strings but you ignore its return
value. You need to do

  text = IncrementAndRebuildInput(text)

to actually capture the output of the function in a variable called text.

>
> #   print "bkpt10"
>
> print
>
> print text#  **  Placing previous inline
> code into function changes result – what am I doing wrong?**


Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] why different result from two similar ways

2012-11-05 Thread Frank Pontius
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@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@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
>>> 
>>>

Re: [Tutor] why different

2006-05-05 Thread linda.s
You are right. When I switch to python23 folder, it works.On 5/5/06, Danny Yoo <[EMAIL PROTECTED]
> wrote:On Fri, 5 May 2006, linda.s wrote:> I have two drives. The python24 is installed in c: and the code is in d:
> drive (d:\data).> so what I did is:> d:\data> c:\python24\python test.pyHi Linda,Can you copy and paste the code to test.py?  I suspect that the code doesnot contain a necessary import statement, but without seeing the code, we
can't really say what's going on.Also, just off-hand: do you have several versions of Python on yoursystem?  I see you have Python 2.4.  Do you know if you have anotherversion of Python installed?
The reason we ask is because of the following scenario: one possibility isthat the Numeric module is installed for another version of Python --- theone you're running with PythonWin --- and that would also explain the
symptoms.  But this is only a possibility; let's learn a little more aboutthe situation.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why different

2006-05-05 Thread Danny Yoo


On Fri, 5 May 2006, linda.s wrote:

> I have two drives. The python24 is installed in c: and the code is in d:
> drive (d:\data).
> so what I did is:
> d:\data> c:\python24\python test.py

Hi Linda,

Can you copy and paste the code to test.py?  I suspect that the code does 
not contain a necessary import statement, but without seeing the code, we 
can't really say what's going on.


Also, just off-hand: do you have several versions of Python on your 
system?  I see you have Python 2.4.  Do you know if you have another 
version of Python installed?

The reason we ask is because of the following scenario: one possibility is 
that the Numeric module is installed for another version of Python --- the 
one you're running with PythonWin --- and that would also explain the 
symptoms.  But this is only a possibility; let's learn a little more about 
the situation.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why different

2006-05-05 Thread linda.s
I have two drives. The python24 is installed in c: and the code is in d: drive (d:\data).
so what I did is:
d:\data> c:\python24\python test.py 
On 5/5/06, Liam Clarke <[EMAIL PROTECTED]> wrote:
Hi,Can you please copy and paste the code here? Also, can you pleaseclick on Start, select Run, and type 
cmd.exe, and in the new windowthat opens type the following command:C:\>echo %PATH%and then right click, select Mark, select the text that was outputtedand press enter to copu it and paste it here also?
Lastly, what directory are you running your code from?Regards,Liam ClarkeOn 5/5/06, linda.s <[EMAIL PROTECTED]> wrote:> I run a code, which import Numeric module. When I run the code from
> PythonWin, it is OK.>  But when I run it from the command line, it reported "Importerror: No> Module named Numeric.">  Why the systems perform differently?>  Linda>
>> ___> Tutor maillist  -  Tutor@python.org> http://mail.python.org/mailman/listinfo/tutor
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] why different

2006-05-05 Thread Liam Clarke
Hi,

Can you please copy and paste the code here? Also, can you please
click on Start, select Run, and type cmd.exe, and in the new window
that opens type the following command:

C:\>echo %PATH%

and then right click, select Mark, select the text that was outputted
and press enter to copu it and paste it here also?

Lastly, what directory are you running your code from?

Regards,

Liam Clarke

On 5/5/06, linda.s <[EMAIL PROTECTED]> wrote:
> I run a code, which import Numeric module. When I run the code from
> PythonWin, it is OK.
>  But when I run it from the command line, it reported "Importerror: No
> Module named Numeric."
>  Why the systems perform differently?
>  Linda
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] why different

2006-05-05 Thread linda.s
I run a code, which import Numeric module. When I run the code from PythonWin, it is OK.
But when I run it from the command line, it reported "Importerror: No Module named Numeric."
Why the systems perform differently?
Linda

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor