By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,667 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

2 line Function doesn't work

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

Expand|Select|Wrap|Line Numbers
  1. def clearup(tor): 
  3.    tor = tor.replace('.', '')
Expand|Select|Wrap|Line Numbers
  1. words =
  3.  for word in words:
  4.       clearup(word)
  5.       if word in dictionary: # dictionary is a tuple of words
  6.          pass
  7.       else:
  8.          print word
why doesn't this work? it prints the words but the periods have not been replaced.
Nov 1 '08 #1
Share this Question
Share on Google+
2 Replies

P: 126
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.
Expand|Select|Wrap|Line Numbers
  1. def clearup(tor): 
  2.    return tor.replace('.', '')
  4. words =
  6.  for word in words:
  7.       word = clearup(word)
  8.       if word in dictionary: # dictionary is a tuple of words
  9.          pass
  10.       else:
  11.          print word
But wont it be simpler to just put is as:
Expand|Select|Wrap|Line Numbers
  1. word.replace('.', '')
Nov 1 '08 #2

P: 30
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.
Nov 2 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.