0

from cs50 import get_string from sys import argv

def main():
    if len(argv) != 2:
        print("Error")
        exit(1)
    wordsbanned = set()

    file = open(argv[1], "r")
    for line in file:
        wordsbanned.add(line.rstrip("\n"))
        file.close

    message = get_string("Enter a message: ")
    token = message.split()



    for t in token:
        while True:
            return str.lower(t)
       if t in wordsbanned:
            print(("*" * len(t)), end=" ")
        else:
            print(t, end=" ")

    print()

if __name__ == "__main__":
    main()
2

Technical aspects of your question:

When posting Python code, make sure indentation is preserved. This looks like you took the code by reddit user /u/randolph_777 and dumped it here, making it pass through two markdown interpreters.

On both platforms, a code block is traditionally marked by four spaces in front of every line. On Stackexchange, in the editor, select the code and click the {} button (Ctrl+K) to add those spaces (assuming you posted the code with correct indentation, this only fixes the four spaces part!), on reddit there's a browser add-on "reddit enhancement suite" for that and other features. The new reddit interface (but not the old one or the mobile app) also supports the style where a single line of triple backticks (```) would be placed above and below the code. On both platforms, there's also inline code, enclosed in single backticks (`code` would turn into code), but that's not to be used for code blocks.

Also, you should probably just post the relevant parts of code, so that it doesn't give away too much. Sure, if it's bleep, there isn't much to give away ;)

Now about your code:

file.close is a function. You need to call it (add parentheses, like file.close()). You could have the file closed automatically by using the with keyword, like

with open(argv[1], "r") as file:
    # place your code here
# close is called automatically

For handling uppercase, you could use if t.lower() in words:, str.lower is a function returning a lower-cased copy of the string.

I don't like that you always print a space after a word, even if it's the last one. What about forming a new list, and " ".join()ing it? Could be a nice list comprehension, a surely Pythonic thing.

2
  • how do i return lower case without printing it?
    – Abhiram Mv
    Jul 29 '19 at 14:26
  • What do you mean? t.lower() would evaluate to a lower-cased copy of the string in t.
    – Blauelf
    Jul 29 '19 at 15:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .