Exc 48 Feedback

Hello Again :slight_smile:,

Finally an exercise I was able to do on my own. However, I would like some feedback if anyone is interested in giving some.

Here is the code:

lexicon = {
     'north': 'direction',
    'south': 'direction',
    'east': 'direction',
    'west': 'direction',
    'go': 'verb',
    'stop': 'verb',
    'look': 'verb',
    'give': 'verb',
    'kill': 'verb',
    'eat': 'verb',
    'the': 'stop',
    'in': 'stop',
    'of': 'stop',
    'from': 'stop',
    'at': 'stop',
    'bear': 'noun',
    'princess': 'noun',
}

def scan(userresponse):
    words = userresponse.split()
    categorized = []

    for word in words:
        try:
            number = int(word)
            numbertype = ('number', number)
            categorized.append(numbertype)
        except ValueError:
            type = (lexicon[word], word)
            categorized.append(type)
        except:
            error = ('error', word)
            categorized.append(error)

    return categorized

I used the ValueError in my try and except sequence because it was a way of making another pathway, but not because I understand what ‘ValueError’ is supposed to be doing. Is there a way of making the code more pretty? I feel like a cheated a little bit.

I read up on the Errors and changed my code to be like this:

lexicon = {
     'north': 'direction',
    'south': 'direction',
    'east': 'direction',
    'west': 'direction',
    'go': 'verb',
    'stop': 'verb',
    'look': 'verb',
    'give': 'verb',
    'kill': 'verb',
    'eat': 'verb',
    'the': 'stop',
    'in': 'stop',
    'of': 'stop',
    'from': 'stop',
    'at': 'stop',
    'bear': 'noun',
    'princess': 'noun',
}

def scan(userresponse):
    words = userresponse.split()
    categorized = []

    for word in words:
        try:
            lword = word.lower()
            type = (lexicon[lword], lword)
            categorized.append(type)
        except:
            try:
                number = int(word)
                numbertype = ('number', number)
                categorized.append(numbertype)
            except:
                error = ('error', word)
                categorized.append(error)

    return categorized

Is there a nice way to do it without using the nested try and except?