This is my implementation of bleep

from cs50 import get_string
from sys import argv, exit

def main():
    if len(argv) != 2:
        print("Usage: python bleep.py <filename.txt>")

    words = set()

    file = open(argv[1], 'r')
    for word in file:

    message = get_string("Input string: ")
    strings = message.split()

    for string in strings:
        for bleep in words:
            if bleep == string:
                string.replace(string, ('*' * len(string)))
        print(string, end=" ")


if __name__ == "__main__":

No matter what Input string I give the program never seems to enter if bleep == string. I wonder if there is a problem in splitting of the message.


From the python doc:

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:

for line in f: 
...     print(line, end='')
    This is the first line of the file.
    Second line of the file

The problem arises because line includes the terminator. It is inferred in this code by use of the end argument in the print statement. Since the banned words will include the terminator (\n), the equality is false. There are myriad ways to solve this problem, I leave that to you.

Spoiler: the replace method will not change the value of string; it returns a new string as per the doc.

You must log in to answer this question.

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