2

i have this failed test when i use check50 in pset6 bleep

:( handles banned words lists with arbitrary words in them

Log

running python bleep.py banned2.txt... sending input My cat and dog are great... checking for output "My *** and *** are great "...

every other test case passed.

This is my code

def main():

    if not len(argv) == 2:
        print("Usage: python bleep.py dictionary")
        exit(1)
    else:
        ban = set()

        # open banned words file to read
        with open("banned.txt", "r") as f:
            # copy words to the set
            for line in f:
                ban.add(line.strip())

        # prompt user for message
        msg = get_string("What message would you like to censor?\n")
        # split msg to words
        msg_words = msg.split(" ")

        msg_censor = ""
        # censor banned words
        for word in msg_words:
            if word.lower() in ban:
                msg_censor += ("*" * len(word)) + " "
            else:
                msg_censor += word + " "

        # print censored msg
        print(msg_censor.strip())

No specification was provided regarding handling banned words lists with arbitrary words in them, what does that mean?

2 Answers 2

6

Is the problem that you have hardcoded the input file to "banned.txt"?

From the specification:

Accepts as its sole command-line argument the name (or path) of a dictionary of banned words (i.e., text file).

Opens and reads from that file the list of words stored therein, one per line, and stores each in a Python data structure for later access. While a Python list will work well for this, you may also find a set useful here.

Perhaps the name of the test is a little clunky, but the program should read the file supplied as an argument, not always "banned.txt". Look carefully at the name of the "dictionary" in the Log file snippet posted here.

1
  • Like DinoCoderSaurus said. For this failed test they have provided a different banned words filed that includes cat and dog, but as you have hard coded banned.txt as the file to open it can't access that file. You need to open whatever file is in argv[1], instead of hard coding 'banned.txt'
    – booklvr
    Mar 11, 2019 at 0:21
1

The block of code that needs to be changed is as mentioned below after the line 15:

save the file name from the command line argument into a variable called banned_text

    banned_text = argv[1]

use the variable name in the below file open command

    with open(banned_text, "r") as f:

With rest of the code as it is, program works absolutely fine:)

You must log in to answer this question.

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