Re: [Tutor] Character Buffer Object Error

2012-02-09 Thread bob gailer

Always reply-all so a copy goes to the tutor list.

On 2/8/2012 11:04 PM, Michael Lewis wrote:

Thanks Bob,

Thanks for what if you did not follow my suggestions?

Your code is still pretty buggy.

Please test it by running it, seeing that the result is not correct, 
then try the desk checking.


The below code is what I came up with without using your suggestion. 
On a scale, how novice is mine compared to what you offered? I am 
curious because I want to know if I should have come up with your 
solution instead of what I did come up with.


def AlterInput(user_input):
'''search for nums in str and increment/append, return new string'''
new_output = []
for num in user_input:
if not num.isdigit():
new_output.append(num)
else:
num.isdigit()
new_num = int(num)
new_num += 1
new_output.append(str(new_num))
return ' '.join(new_output)

def GetUserInput():
'''Get a string from the user and pass it'''
user_input = '''I * got 432 when I counted, but Jim got 433 which
is a lot for only 6 cats, or were there 12 cats?'''
return AlterInput(user_input.split())


Your code:

return ' '.join(str(int(num)+1) if num.isdigit() else num for num in 
user_input)


On Wed, Feb 8, 2012 at 6:04 AM, bob gailer bgai...@gmail.com 
mailto:bgai...@gmail.com wrote:


On 2/8/2012 12:56 AM, Michael Lewis wrote:

I want to find all digits in a string and then increment those
digits by 1 and then return the same string with the
incremented digits.

[snip]

You got lots of good advice from others and some not-so-good advice.


Michael said:
2. The following line appears wrong.
new_output = ' '.join(user_input)
He did not say why!


I add:

Master the art of Desk Checking.

Take pencil  paper.

Write down the input and output of each statement as though you
were the computer.


Also learn to read the  understand the Python Manuals:

str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old
replaced by new. If the optional argument count is given, only the
first count occurrences are replaced.

Notice return a copy. It does NOT say that this will convert old
in place.

No one else caught this problem!

Since this is homework we we probably should not be offering
alternative solutions.

However I can't resist offering the one-line solution:

''.join(str(int(x)+1) if x.isdigit() else x for x in 'user_input)

-- 
Bob Gailer

919-636-4239 tel:919-636-4239
Chapel Hill NC





--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Character Buffer Object Error OOPS

2012-02-09 Thread bob gailer

On 2/8/2012 11:04 PM, Michael Lewis wrote:

Thanks Bob,


My Bad - I did not examine all your code - I broke my own rule.

Your code is still a little buggy.

The below code is what I came up with without using your suggestion. 
On a scale, how novice is mine compared to what you offered? I am 
curious because I want to know if I should have come up with your 
solution instead of what I did come up with.


Since you are studying Python you do well to apply what you learn. The 
first objective is to get something that works.


As you learn more you then apply new techniques.

When you have learned conditional expressions, list comprehensions and 
generator expressions then you will be able to write the code I did.


That code is just an alternative to what you wrote, flattening it as 
it were.

[snip]

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Character Buffer Object Error

2012-02-09 Thread Michael Lewis
On Thu, Feb 9, 2012 at 2:43 PM, bob gailer bgai...@gmail.com wrote:

 Always reply-all so a copy goes to the tutor list.

 On 2/8/2012 11:04 PM, Michael Lewis wrote:

 Thanks Bob,

 Thanks for what if you did not follow my suggestions?

I partially followed your suggestions by getting rid of str.replace(old,
new[, count]) and new_output = ' '.join(user_input). But I also wanted to
try to get this to work without simply copying your code.


 Your code is still pretty buggy.

   I don't think I made my intentions clear. I don't wont to increment
each digit. I want to find each number (for example, if the str contains
436, I don't want to interpret that as 4, 3, 6 - I instead want to
interpret that as 436, which is why I created a list and then joined back
into a string afterwards.


 Please test it by running it, seeing that the result is not correct, then
 try the desk checking.
 I don't see how this is not correct, because my input was



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


And my output was:

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

The below code is what I came up with without using your suggestion.
 On a scale, how novice is mine compared to what you offered? I am curious
 because I want to know if I should have come up with your solution instead
 of what I did come up with.

  def AlterInput(user_input):
 '''search for nums in str and increment/append, return new string'''
 new_output = []
 for num in user_input:
 if not num.isdigit():
 new_output.append(num)
 else:
 num.isdigit()
 new_num = int(num)
 new_num += 1
 new_output.append(str(new_num))
 return ' '.join(new_output)

 def GetUserInput():
 '''Get a string from the user and pass it'''
 user_input = '''I * got 432 when I counted, but Jim got 433 which
 is a lot for only 6 cats, or were there 12 cats?'''
 return AlterInput(user_input.split())


 Your code:

 return ' '.join(str(int(num)+1) if num.isdigit() else num for num in
 user_input)


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


Re: [Tutor] Character Buffer Object Error

2012-02-08 Thread Alan Gauld

On 08/02/12 05:56, Michael Lewis wrote:


I've tried the following code, but I am getting the following error. How
do I do this properly?


Christian answered the reason for the error.
Here are some other comments...


def AlterInput(user_input):
 print user_input
 new_output = ''


make this a list not a string


 for index, char in enumerate(user_input):


you don;t need index or enumerate just a simple for loop.


 if char.isdigit():
 new_char = int(char)
 new_char += 1


How will you handle 9? Insert 10 or lose the 1 (or the zero?)
Your choice... I'll assume you insert 10


 new_output = ' '.join(user_input)


I gave no idea what this is for, why insert spaces?
Instead append new_char as a str to the list
new_output.append(str(new_char))

This needs to be outside the if statement because
you want to add all the chars. You will need to play with the 
assignments to make that work but it should be a lot simpler...


At the very end join the list back to a string:

return ''.join(new_output)


 new_output.replace(char, new_char)


You don't need this with a list


 print new_output


Best to keep print statements outside the function and just return the value



 AlterInput(user_input.split())


You don't need to split, just pass in the users string.

HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Character Buffer Object Error

2012-02-08 Thread bob gailer

On 2/8/2012 12:56 AM, Michael Lewis wrote:
I want to find all digits in a string and then increment those digits 
by 1 and then return the same string with the incremented digits.

[snip]

You got lots of good advice from others and some not-so-good advice.

Michael said:
2. The following line appears wrong.
 new_output = ' '.join(user_input)
He did not say why!


I add:

Master the art of Desk Checking.

Take pencil  paper.

Write down the input and output of each statement as though you were the 
computer.



Also learn to read the  understand the Python Manuals:

str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old 
replaced by new. If the optional argument count is given, only the first 
count occurrences are replaced.


Notice return a copy. It does NOT say that this will convert old in place.

No one else caught this problem!

Since this is homework we we probably should not be offering alternative 
solutions.


However I can't resist offering the one-line solution:

''.join(str(int(x)+1) if x.isdigit() else x for x in 'user_input)

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


[Tutor] Character Buffer Object Error

2012-02-07 Thread Michael Lewis
I want to find all digits in a string and then increment those digits by 1
and then return the same string with the incremented digits.

I've tried the following code, but I am getting the following error. How do
I do this properly?

def AlterInput(user_input):
print user_input
new_output = ''
for index, char in enumerate(user_input):
if char.isdigit():
new_char = int(char)
new_char += 1
new_output = ' '.join(user_input)
new_output.replace(char, new_char)
print new_output

def GetUserInput():
'''Get a string from the user and pass it'''
user_input = '''I got 432 when I counted, but Jim got 433 which
is a lot for only 6 cats, or were there 12 cats?'''
AlterInput(user_input.split())


Traceback (most recent call last):
  File C:/Python27/Homework/Homework 4_1.py, line 25, in module
GetUserInput()
  File C:/Python27/Homework/Homework 4_1.py, line 23, in GetUserInput
AlterInput(user_input.split())
  File C:/Python27/Homework/Homework 4_1.py, line 15, in AlterInput
new_output.replace(char, new_char)
TypeError: expected a character buffer object

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


Re: [Tutor] Character Buffer Object Error

2012-02-07 Thread Christian Witts

On 2012/02/08 07:56 AM, Michael Lewis wrote:
I want to find all digits in a string and then increment those digits 
by 1 and then return the same string with the incremented digits.


I've tried the following code, but I am getting the following error. 
How do I do this properly?


def AlterInput(user_input):
print user_input
new_output = ''
for index, char in enumerate(user_input):
if char.isdigit():
new_char = int(char)
new_char += 1
new_output = ' '.join(user_input)
new_output.replace(char, new_char)
print new_output

def GetUserInput():
'''Get a string from the user and pass it'''
user_input = '''I got 432 when I counted, but Jim got 433 which
is a lot for only 6 cats, or were there 12 cats?'''
AlterInput(user_input.split())


Traceback (most recent call last):
  File C:/Python27/Homework/Homework 4_1.py, line 25, in module
GetUserInput()
  File C:/Python27/Homework/Homework 4_1.py, line 23, in GetUserInput
AlterInput(user_input.split())
  File C:/Python27/Homework/Homework 4_1.py, line 15, in AlterInput
new_output.replace(char, new_char)
TypeError: expected a character buffer object

Thanks.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
new_char is of type int and not type str, so cast it back to a string 
before using it in your call to .replace as it expects both arguments to 
be strings.

--

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


Re: [Tutor] Character Buffer Object Error

2012-02-07 Thread Asokan Pichai
Dear Michael

Overall I see  a few problems.

0. Functions should return values. Not print them

1. Why use enumerate? The code is not using the index anyway

2. The following line appears wrong.
 new_output = ' '.join(user_input)

3. This line has a very buggy possibility.
 new_output.replace(char, new_char)
Let us say you have 234. First time you will replace 2 with 3, getting 334.
And then you will replace the first 3 with 4 getting 434 and finally
end up with 534. Of course due to other coding errors this does not
come to pass. But your basic idea of using str.replace() is prone
to this problem.

4. How are you supposed to treat 9?

Anyway back to your problem:

 I want to find all digits in a string and then increment those digits by 1
 and then return the same string with the incremented digits.

Pseudocode
--
Initialise and outstring (empty)
Read the instring character by character
if the current character is not a digit
append it to outstring
else
append the transform(current char) to outstring

If you can organize your code like this it may be easier

HTH
Regards

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