10/15 - Is this ok?


Hey there,

i am completely new to python and/or programming as a whole and i obviously still don’t even understand what i am doing ^^

could someone please tell me if my solution is okay or if there are any problems with it? i altered it for quite some time now … and that kinda got me really confused, to the point i was about to quit trying…i am not sure if for example all lines are necessary.

def censor(text, word):
  word_list = text.split()
  cen_list = []
  length = len(word_list)
  for i in range(0,length):
    if word_list[i] in word:
      cen_list.append("*" * len(word))
  result = " ".join(cen_list)
  return result

Greetings from Austria and thanks in advance!

PS: sorry for any major mistakes in my post, it’s not my native language (and it isn’t python either :smile: )


Looks good to me, if I was to look for things to complain about:

  • 4 spaces preferred over 2 (by pep8 which is what the python community usually refers to for style)
  • Calling range with one argument starts at 0 and counts up to that value, making 0 unneccessary
  • You don’t specifically need to know the positions, so rather than iterating over a list of indexes, loop over the list of words
  • rather than creating a variable for the result, return the result of the expression directly

If each and every thing has a purpose you can argue for then it’s all good


Oh. This I actually disagree with, because it’s wrong/doesn’t say the right thing. I could give an example where it gives unexpected results


Thanks for the extremely fast reply, ionatan

I was wondering about the 4spaces indents, since i am pretty sure even codecademy mentions something along those lines. i just went with the 2 spaces since that is what hitting enter gave me :smiley:



if word_list[i] == word

would be the right one?


Codecademy’s editor is misconfigured for the tab key and some code pices start with 2 spaces or maybe it’s the editor doing that too, yeah.

It’s conformity with everyone else really, you’re perfectly allowed to use other amounts. But it’s really nice when most everyone aims for the same style unless they find good reason not to :^)

Yes, equal rather than contains a substring


Thanks again for the input. Much appreciated.
i surely wanna stay true to the styles of the majority :slight_smile:

Now on to the next “challenge”, hopefully this time i won’t need so much time for what i guess are very easy tasks for someone a bit more experienced.


To make these solutions faster you need to know how basic types behave (lists, strings, numbers, dicts, etc), then you decide which actions should happen (almost entirely unrelated to code), and once that’s done you can start writing, you should be writing exactly those steps (no trial and error or changing things because it isn’t “allowed”) what might happen is that you start using something that doesn’t match your plan (like using a string and discovering that you can’t change individual characters) (in which case, pick a different data type, like list, but stick with the same plan) or that your plan involves something that isn’t easily described in code (you didn’t think it through in sufficient detail and discovered that you can’t actually describe it, that’s a broken plan)

And if you then write write the code for each such action one after the other, you should be going at a decent pace. (You may want to do a bit of testing after each implemented step such as printing out the current state of the program)


i think i get what you’re saying…
i had a lot of trial and error with the last one because my “idea” was the wrong one, or i lack the knowledge to turn it into a working code.

but in hindsight… i studied many of the functions and learned a lot through that.

the next one took me about 2-3 minutes of thinking and i turned up immediatly with this

def count(sequence,item):
    length = len(sequence)
    count = 0
    for i in range(length):
        if sequence[i] == item:
            count += 1
            count += 0
    return count

which seems to work… and i never even had to change a thing… makes me proud… (even though i know it’s not special)


Again with indexes, don’t need to create a list of numbers (range) and iterate through that to iterate through your list. Iterate through your list instead.

Indexes are a common source of bugs, it’s a bunch of cognitive overhead instead of just saying “all”

Adding zero does nothing, so simply don’t

What I was going for there was that thinking about actions isn’t tied to writing code, and when you know what should happen it should be much easier to write the code for that, especially if focusing on a single action at a time


oh for some reason i though i needed an else-statement in there. ■■■■ it… there goes my proud moment :roll_eyes:.

i had trouble understanding the whole “range” thing, and it was mentioned that those have the benefit of beeing able to change the list afterwards (even though i know it’s not necessary here)
that was the reason i decided to use it more…

but ofc i understand…

so i changed what you mentioned to this:

def count(sequence,item):
    count = 0
    for number in sequence:
        if number == item:
            count += 1
    print count
    return count

thanks once more


It just creates a list

>>> range(5)
[0, 1, 2, 3, 4]

But you already have a list, don’t need another.

So if you specifically need to know locations (such as replacing the value there) then you might need range. Most of the time it’s cleaner to create a new list instead, so if you for example were to turn the list into 0s and 1s and later sum them, you could do:

item = ...
stuff = ...
for i in range(len(stuff)):
    stuff[i] = 1 if stuff[i] == item else 0

And that really just makes me squint and scratch my head so I’d rather:

item = ...
stuff = ...
result = []
for thing in stuff:
    result.append(1 if thing == item else 0)

Pardon the vague variable names. “item stuff thing”


i didn’t even realize i could put stuff like range(len(stuff)) in my code… i always declared a new variable for that.
same with your if-statement inside the append command.

very informative and useful indeed.

i’m astonished by how much progress i think i made just because of our small-talk here.
getting stuff done faster, code getting smaller and smaller, main reason beeing that i finally understand stuff like range and indexes… just great!
the next one was even more simplistic after the newfound knowledge you’ve thaught me

thanks a lot ionatan!

ps: if you really think those are bad variable names… you defintely shouldn’t read most of mine :smiley:


It’s not a statement (statements aren’t allowed there)
A Statement does something, an expression evaluates into a value
It’s something completely separate from an if-statement. (It’s called a ternary expression)
There are lots and lots of such little tricks/shortcuts/syntactic sugar, and none of them are required.
Loops, lists, dicts, numbers, strings, functions create pretty much everything else.


sorry for that.
it’s kinda hard for me to understand some of the “advanced” english vocabulary, so mistakes like that happen to me :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.