I really thought it would convert this time. Does anyone see why it wasn’t able to convert?
I can be wrong but I believe the top section is good. it’s at the very bottom, in the elif
section where I’m having trouble
this is my code:
lexicon = {
'north': 'direction',
'south': 'direction',
'east': 'direction',
'west': 'direction',
'go': 'verb',
'kill': 'verb',
'eat' : 'verb',
'the': 'stop',
'in': 'stop',
'of': 'stop',
'bear': 'noun',
'princess': 'noun',
'1234': 'number',
'3': 'number',
'91234': 'number',
'ASDFADFASDF': 'error',
'bear': 'noun',
'IAS': 'error',
'princess': 'noun',
}
def convert_number(s):
try:
return int(s)
except ValueError:
return None
def scan(sentence):
words = sentence.split()
result = []
for word in words:
if lexicon[word] == 'noun' or 'verb' or 'stop' or 'error':
pair = (lexicon[word], word)
result.append(pair)
here’s the part I’m having issues with and I can’t figure out why
basically, I say, call the value associated to the key [word]
from the dict lexicon
and if it equals 'number'
run the branches in this block of code
than I convert the string-number (‘1234’) using the convert_number function and pass it to integer
than I create a tuple pair from lexicon[word]
and integer
, which should print out: ('number', 1234
). Than pass this tuple to number_pair
and finally append number_pair
to result
`elif lexicon[word] == 'number':
integer = convert_number(word)
number_pair = (lexicon[word], integer)
result.append(number_pair)`
return result
I’ve been going at this for many hours trying to convert the string-number passed to the scan
function argument into an integer. I really thought I would get it this time but I still got an AssertionError.
Does anybody see where I went wrong in this?
this is the assertion error (as you can see the, actual object is still a string and the desired object is an integer):
AssertionError: Lists differ: ['(number)'], '1234')] != [('number', 1234)]