Censor Dispenser

I have taken on the extra task “censor Dispenser”

I checked some solutions, but i can’t figure out, why mine does not work for email_two. Works fine for email_one

You create a generator expression with this line of code (email_two_cens = (censor(email_two, entry) for entry in proprietary_terms) but you never evaluate it. See the following for an example.

gen_expr = (x for x in range(10))
print(gen_expr)
Out: <generator object <genexpr> at #address#>
# You must evaluate/unpack the generator in some way before passing it to print.

What did you want from that line? Even a list wouldn’t actually meet the requirements, nor would the moving the expression into a string with the ''.join() method. You need to be returning a single string if I’m not mistaken. Make sure you keep that first string and keep altering it.

Perhaps consider rewriting this line entirely to use the output of your censor function for each new word in proprietary_terms.

1 Like

thank you! But i do not actually get the problem here…would your example work? Maybe i should rewrite the function to return something differently

1 Like

I didn’t actually provide an example as ideally you’d be completing the requirements with as little external input as possible. There’s more than one way to solve this problem but consider what .replace() actually returns and then work out how you might censor multiple words using that method. Perhaps start with censoring two different words from the same string without loops, functions or anything else (even on pen and paper is a good idea), once that’s sorted just expand that thinking into a more practical solution for numerous repeats.

2 Likes

Thank you!
But i still do not really understand, why my approach works for email_one, but does not for email_two.
It would be nice to understand before i start working on a different approach.

Based on what you want to do your current code tells the computer to do something entirely different as described below. I think it would be easier if you used standard loops for these problems until you are more comfortable with them. If you’re curious about what your code does then you can read on but it may not be too helpful at this point.

I’m not certain what your expectation was of this line: email_two_cens = (censor(email_two, entry) for entry in proprietary_terms); it’s a generator expression which I don’t believe you intended.

They’re just a convenient method of producing your own iterators with a similar to syntax to list comprehensions. If that’s not something you’ve looked into before you can find details here- https://docs.python.org/3/howto/functional.html#iterators but it’s unlikely to help you solve this particular problem.

You can view the details at the pep update where they were included here- https://www.python.org/dev/peps/pep-0289/

Here’s a very brief and very impractical example of how a generator expression might be used.

gen_expr = (x for x in range(3))
for value in gen_expr:
    print(value)


0  # values are output
1
2

I can’t see a route through which creating this generator object would be helpful to you solving your problem so I’d suggest using what you’ve already covered in lessons and avoiding this particular syntax for the time being.