Ex 48.not able to solve

whats wrong with this code.
help me in solving this and i tried to use dictionary but not able to solve.
anyone solved using dictionaries post that code too.

direction = ('north', 'south', 'east')
verb = ('go', 'kill', 'eat')
stop = ('the', 'in', 'of')
noun = ('bear', 'princess')
error=('IAS','ASDFADFASDF')
number=(3,1234,91234)

def convert_number(s):
    try:
       return int(s)
   except ValueError:
       return None

def scan(sentence):
  words = sentence.split()
  empt=[]

  for word in words:
 	if word in direction:
    	  empt.append('direction', word)
          return empt

    elif word in verbs:
             empt.append('verb', word)
             return empt

    elif word in stop:
             empt.append('stop', word)
             print empt

    elif word in nouns:
            empt.append('noun', word)
            print empt

    elif convert_number(word) in number:
          empt.append('number', convert_number(word))
           return empt
    else:
        return('error', word)

What exactly is the error that you’re getting when running it?

I see differences for indentation. For example, in the convert_number function, I see a difference for indentation between the lines “try:” and “except ValueError:”
Here’s what I get in the interpreter in the Terminal:

>>> def convert_number(s):
...     try:
...        return int(s)
...    except ValueError:
  File "<stdin>", line 4
    except ValueError:
                     ^
IndentationError: unindent does not match any outer indentation level

There is only a difference of indentation of one space between “try:” and “except ValueError:”.

I did it correctly.while pasting the code it got wrong.

Hey @hemanth, you appear to be thinking about this backwards from what you should do. To use a dict, you have to map from the word to the type of word. You however are mapping in the reverse with your list of variables. So, think this way:

lexicon = {'go': 'verb', 'eat': 'verb', 'north': 'direction'}
word_type = lexicon.get('go')

See how now I can give the lexicon dict whatever word I want and it will return the correct word type?

1 Like