tags:

views:

115

answers:

4

This is my first effort on solving the exercise. I gotta say, I'm kind of liking Python. :D

# D. verbing
# Given a string, if its length is at least 3,
# add 'ing' to its end.
# Unless it already ends in 'ing', in which case
# add 'ly' instead.
# If the string length is less than 3, leave it unchanged.
# Return the resulting string.
def verbing(s):
  if len(s) >= 3:
    if s[-3:] == "ing":
      s += "ly"
    else:
      s += "ing"
    return s
  else:
    return s 

  # +++your code here+++
  return

What do you think I could improve on here?

A: 

Pretty good for a beginner! Yes, I would say this is the Pythonic way of doing things. I especially like the way you have commented exactly what the function does. Good work there.

Keep working with Python, though. You're doing fine.

George Edison
It looks to me like the commentary was his assignment/exercise copy-pasted.
Wallacoloo
Although documentation for functions goes in docstrings.
keturn
@George: This at least the THIRD assignment that he's copy/pasted, all using a template starting with an assignment ID and name (e.g. `# D. verbing` and ending with ` # +++your code here+++\n return`
John Machin
+6  A: 
def verbing(s):
  if len(s) >= 3:
    if s.endswith("ing"):
      s += "ly"
    else:
      s += "ing"
  return s
Ignacio Vazquez-Abrams
Alternately, `s += "ly" if s.endswith("ing") else "ing"` but it's debatable which is more "readable" or "better" in this case.
Chris Lutz
+1  A: 

How about this little rewrite:

def verbing(s):
    if len(s) < 3:
        return s
    elif s.endswith('ing'):
        return s + 'ly'
    else:
        return s + 'ing'
WoLpH
Technically, the `elif` could be a plain `if` and the `else` at the end isn't necessary at all, because we're returning before flow control can continue in those cases.
Chris Lutz
Indeed, but I personally find it more readable if all options are in the same `if`, `elif`, `else` block.
WoLpH
+1  A: 

I would use s.endswith("ing") in the if, which is also a bit faster, because it doesn't create a new string for the comparision.

And second, I would use docstrings for commenting. This way, you can see your description when you do a help(yourmodule) or when you use some autodoc-tool like Sphinx to create a handbook describing your API. Example:

def verbings(s):
    """Given a string, if its length is at least 3, add 'ing' to its end.
    Unless it already ends in 'ing', in which case add 'ly' instead.
    If the string length is less than 3, leave it unchanged."""
    # rest of the function

Third, it's often considered a bad practice to change input parameters. You can do it for dict or list parameters, which can also act as output parameters. But strings are input parameters only (that's why you have the return). The source you have written is valid of course, but is often confusing. Other languages have often a final or const keyword to avoid this confusion, but Python doesn't. So, I would recommend you, to use either a second variable result = s + "ing" and do a return result afterwards, or write return s + "ing".

The rest is perfectly fine. There are of course some constructs in Python which are shorter to write (you will learn them with the time), but they are often not so readable. Therefore I would stay with your solution.

tux21b
Strings in Python are immutable, so changing the input value won't change the string the OP passed in. Fix that mistake and I'll +1 you.
Chris Lutz
I know that strings are immutable (that's also the reason why you can't use a string as output parameter). It's just harder to follow the source in the function body when you rebind the value of the input parameter. But feel free to edit if you think that a sentence is incorrect...
tux21b