1

There's a lot out there on similar topics, but nothing I've found (on this website or elsewhere) seems to answer my question. That's because I want to do this with a for loop; not by importing modules like string, or using regexes, etc.

I have a tuple consisting of some characters I don't like.

A string is then checked against this tuple, the idea being that if these forbidden characters are elements in the string, they'll be removed.

get_rid_of_us = ('.', '"', '?', ' ')

def fix_me(text):
    text = ''.join(text.split()) # removing whitespace
    text = text.lower() # making it lowercase
    for i in get_rid_of_us:
        if i in text:
            text = text.replace(i, '')
            return text
        else:
            return text

where text is user input from elsewhere in the script, and goes on to be used afterwards. Something is wrong with my for statement - what am I missing? Thanks :)

platov
  • 35
  • 8
  • whats the exact error or incorrect return your seeing? – Lucas Hendren Sep 10 '17 at 03:33
  • What is an example input `text` and the desired output? – BoltzmannBrain Sep 10 '17 at 03:35
  • @Legman ah, sorry I didn't make that clear. A string passed through the function that had multiple forbidden elements (i.e. `"foo,b.ar!"`) would not see these elements removed, as intended. – platov Sep 10 '17 at 05:24
  • @BoltzmannBrain if input were `" FOO, b.ar!"`, the first two lines of the function would (correctly) change it to `"foo,b.ar!"`. But at the next step, the for loop would not properly remove the forbidden characters specified in my tuple. – platov Sep 10 '17 at 05:27

3 Answers3

0

I don't program in python, so I won't write the exact code, but I can explain what is wrong here. You don't need the else statement, and you should remove the return statements from inside the for loop. That way you can process all members of get_rid_of_us before returning the processed text. The return statement should come after the loop has completed. Let me know if any of this is not clear.

Mozahler
  • 4,958
  • 6
  • 36
  • 56
0

Here's a demonstration that probably shows what you want based on the StackOverflow answer Remove specific characters from a string in python

$ python
Python 2.7.12 (default, Nov 29 2016, 14:57:54) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> text = "Python 2.7.12 (default, Nov 29 2016, 14:57:54) "
>>> get_rid_of_us = ('.', '"', '?', ' ')
>>> text.translate(None, "".join(get_rid_of_us))
'Python2712(default,Nov292016,14:57:54)'

so instead of your loop you could write

def fix_me(text):
    text = ''.join(text.split()) # removing whitespace
    text = text.lower() # making it lowercase
    text = text.translate(None, "".join(get_rid_of_us))
jq170727
  • 13,159
  • 3
  • 46
  • 56
  • Thank you -- I did read through that thread, but I was hell-bent on getting it done using a for loop. Your suggestion is probably more efficient. Cheers :) – platov Sep 10 '17 at 05:31
0

You are returning from your function on the first run of your loop. Also you don't need else part.

    get_rid_of_us = ('.', '"', '?', ' ')

    def fix_me(text):
        text = ''.join(text.split()) # removing whitespace
        text = text.lower() # making it lowercase
        for i in get_rid_of_us:
            if i in text:
                text = text.replace(i, '')
        return text

Or you can use built in functions to do the same

get_rid_of_us = ('.', '"', '?', ' ')

def fix_me(text):
    filter(lambda i: i not in get_rid_of_us,text)
Rahul K
  • 665
  • 10
  • 25
  • Solved my problem, thank you. Especially glad to learn another approach from the second part of your answer :) – platov Sep 10 '17 at 05:34