Note that there are some explanatory texts on larger screens.

plurals
  1. POLPTHW Excercise 48 Help - Working with Tuples in Lists
    text
    copied!<p>I'm currently going through LPTHW and I'm up to <a href="http://learnpythonthehardway.org/book/ex48.html" rel="nofollow noreferrer">excercise 48</a> and it's the first time I've hit a brick wall. </p> <p>Here's the first part of the test case I've been given</p> <pre><code>from nose.tools import * from ex48 import lexicon def test_direction(): assert_equal(lexicon.scan("north"), [('direction', 'north')]) result = lexicon.scan("north south east") assert_equal(result, [('direction', 'north'), ('direction', 'south'), ('direction', 'east')]) </code></pre> <p><a href="https://stackoverflow.com/questions/6881193/learn-python-the-hard-way-exercise-48-help">This question has been asked here before</a>, and I noticed my current solution so far is pretty identical to the <a href="https://stackoverflow.com/questions/6881193/learn-python-the-hard-way-exercise-48-help/7241227#7241227">answer provided by robbyt</a>. Yet it still doesn't work.</p> <pre><code>def scan(thewords): directions = [('direction', 'north'), ('direction', 'south'), ('direction', 'east')] thewords = thewords.split() sentence = [] for i in thewords: if i in directions: sentence.append(('direction', i)) else: sentence.append(('error', i)) return sentence </code></pre> <p><strong>So the question is: After taking the input (thewords), how do I search through the list of tuples correctly and then return the specific tuple it's a part of?</strong></p> <p>Thanks in advance for any sort answers and advice, really stuck with this one.</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