Hello Again ,
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.