Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're doing the replace but not storing the result anywhere, so it is lost. I assume you mean something like this:</p> <pre><code>sentence = sentence.replace(chamber,'') </code></pre> <p>instead of:</p> <pre><code>sentence.replace(chamber,'') </code></pre> <p>Strings are immutable in Python and so can't be modfied "in place." A new string is always created and so must be assigned to a name if it is to be retained.</p> <p>Your <code>while</code> loop will only exit (via the <code>return</code> statement) if it doesn't find <code>&lt;</code> in the text, and this will never happen unless the text doesn't contain a <code>&lt;</code> character to begin with since, as just described, you are never changing the value of <code>sentence</code>.</p> <p>I'll also mention that your <code>elif</code> is redundant. You can just use <code>else</code> there and omit the condition, since it's the opposite of the one in your <code>if</code>. Furthermore, there's no need for the <code>state</code> variable or <code>list</code> for that matter (and that's a bad name to begin with).</p> <p>I'd rewrite your function something like this:</p> <pre><code>def remove_tags(html): while '&lt;' in html: start = html.find('&lt;') end = html.find('&gt;', start) tag = html[start:end+1] html = html.replace(tag, '', 1) return html.split() </code></pre> <p>I have also changed your variable names to better reflect what they're used for.</p> <p>Of course, this little function will fail to strip what you want if you have any <code>&gt;</code> symbols in attributes of your HTML tags. Instead, consider using an HTML parser such as BeautifulSoup to do this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload