0

I am encountering a problem in CS50 Bleep. When censoring the message, the censored message consists of only one * for the entire word, rather than the length of the word. Here is my code:

from cs50 import get_string
import sys

def main():
    if not len(sys.argv) == 2:
        print("Usage: python bleep.py dictionary")
        exit(1)
    else:
        text = open(sys.argv[1], "r")

    bannedword = set()
    for line in text:
        bannedword.add(line.strip("\n"))

    input = get_string("What message would you like to censor?")

    input_word = input.split()



    for word in input_word:
        if word.lower in bannedword:
            print("*" * len(word), end = " ")
        else:
            print(word, end = " ")
if __name__ == "__main__":
    main()
0

I don't think this code bleeps anything. Call lower by adding parentheses: if word.lower() in ...

BTW, you don't close your file, so it stays open until Python terminates. You can use with, like

with open(sys.argv[1], "r") as text:
    for line in text:
        ...

with will ensure the resource is closed after the block.

| improve this answer | |
  • I was so stupid to forget the parentheses... Thanks so much! – jsyf May 23 '19 at 10:23

You must log in to answer this question.

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