0

I assume that I have the following problem: I create a list for the banned words and although a list with the text input. Then I want to compare both list.

But when I create the list of the user input - I think - it stores the complete string and index 0 of the list and afterwards I cant compare both lists.

I made a screenshot of how the list look like, after I stored the string in it:

enter image description here

And here is my code:

from cs50 import get_string
from sys import argv


def main():

if len(argv) != 2:
    while True:
        print("Enter only filename of dictionary")
        if len(argv) == 2:
            break

filename = argv[1]
try:
    file = open("{}".format(filename), "r")
except IOError:
    print("Could not open file!")

bannedList = []

for line in file:
    s = line.splitlines()
    bannedList.append(s)
file.close()

input = get_string("Enter your text: ")
inputList = [input.split()]

rightList = ""

for word in input:
    if word in bannedList:
        rightList += ("*" * len(word)) + " "
    else:
        rightList += word + " "


print(rightList.strip())

if __name__ == "__main__":
    main()

Thanks for your help!! :)

1 Answer 1

1

Firstly, the method split returns a list; and as per the line inputList = [input.split()] your're enclosing the returned list within another which yields the nested list.

Secondly, you might want to consider revising the line s = line.splitlines() as the method splitlines breaks the string at line boundaries (i.e. \n) which might not what you intended since the parameterised line is a single line with no line boundaries.

1
  • Thanks! Solved the problem with the list of banned words, by reading every line and using strip to delete the "\n" And with the input text I just created a list directly with split. Thanks!
    – Dawienchi
    Apr 19, 2019 at 7:33

You must log in to answer this question.

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