2 line Function doesn't work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bouzy
    New Member
    • Jun 2007
    • 30

    2 line Function doesn't work

    I am trying to go through words in a list of words and take out all the '.'s. I made the function ...

    Code:
    def clearup(tor): 
    
       tor = tor.replace('.', '')
    Code:
    words = fcheck.read().split()
    
     for word in words:
          clearup(word)
          if word in dictionary: # dictionary is a tuple of words
             pass
          else:
             print word
    why doesn't this work? it prints the words but the periods have not been replaced.
  • Smygis
    New Member
    • Jun 2007
    • 126

    #2
    What happens is that you got a function that modify a copy of a string and then don't do anything with it.

    This is one way of how you can do it instead.
    Code:
    def clearup(tor): 
       return tor.replace('.', '')
    
    words = fcheck.read().split()
    
     for word in words:
          word = clearup(word)
          if word in dictionary: # dictionary is a tuple of words
             pass
          else:
             print word
    But wont it be simpler to just put is as:
    Code:
    word.replace('.', '')

    Comment

    • Bouzy
      New Member
      • Jun 2007
      • 30

      #3
      Thanks, with your help and some other things I did I got it fixed, but the reason I didn't use your second example is because I was going to reuse the function because it's replacing not just ','s but every punctuation mark in the English language.

      Problem solved.

      Comment

      Working...