EX48 is this code style right?

Hello people. I would like to ask experienced programmers (and Zed himself) if my programmer style is at least average. There are some flaws like one list for one type of words. Sorry for my grammar, english is not my first language. I look forward to your tips.

This code is written to pass test. All is written in english so I think it’s readable. 42 lines long
Source code:
Edit: Code is updated. It is almost completely rewriten.
Edit2: Another update. Hope it’s better than best @zedshaw

lexicon = {'north':'direction',
def scan(words):
    breaked_words = words.split()
    result = []
    for key in breaked_words:
        item = lexicon.get(key)
            if item:
                prepared = item, key
                prepared = 'number', int(key)

        except ValueError:
            prepared = 'error', key

    return result

Did you watch the video for ex48, assuming you can access? The first potential issue is using lists rather than a dict data structure.

But the style approach I’d consider addressing is the amount of repetition. When you see repetition, consider refactoring. It makes you code tidier, more readable and easier to maintain.

Hey @Lt.Dan, I edited your post for code formatting. Put [code]...[/code] around your code and it’ll format.

So, as @gpkesley said you’re using a bunch of lists instead of a dict. Go back and study Ex39 to remind yourself how the dict works. Then, remember you have the .get function on dicts that will get you an element by its key, or return None.

Come back with a new attempt and I’ll comment on that one (and remember the [code]).

Very very close. Ok so move the lexicon out of this function and above it. No need to put it there and make it bigger and more confusing that it is.

Now, you don’t need tuple() at all for anything. Skip that. And you also don’t need lexicon_keys since you don’t even use it, and a dict is already better at telling if something is in your set of keys than a tuple is. Once you’ve done that then:

You can use this to simplify it even more:

word_type = lexicon.get(key)

The .get on a dictionary will try to look up the key, and if it doesn’t find one, return None. Then after that you can try to convert it to an integer, or give an error.

The other option is to use the exception:

    word_type = lexicon[key]
except KeyError:
    # try number convert or error here

Lastly, don’t put the numbers into your lexicon. That’ll make the test work, but then you’ll want to take those out and do number conversion. You can test if a string is a number with:


Try that out. Very close. I think one more day of hacking on this and you’ll get it. I also think you might benefit from doing the results oriented coding method I advocate. It looks like you’re putting code in based on what you’re thinking right? So you’re trying to “speak python” right away. It’s better to write English and then translate to python:

# create the dict for the lexicon

def scan(words):
    # break up the sentence by spaces
    # for each word in the sentence
    # try to get it
    # if it doesn't exist, try to make a number
    # otherwise it's an error

Now you just have to put python under those comments to make it work. I’d say you might want to scrap this code and consider it a study, then using my comments above try rewriting it based on what you’ve learned, this code, and what my plan is there. Give that a shot too. I find sometimes throwing bad code out and starting over makes the second attempt even easier and better.