I have recently completed the code challenge for exercise 48 in Learn Python3 the Hard Way. I was looking for any feedback on my solution and any ideas for improvement!
The full task can be found here:
The project involved taking the test code and creating a script that works from it. The test code can be found here (not my upload).
It passes all the tests, but after at other solutions I doubt my solution. Is my solution effective / efficient?
def scan(sentence): rebuild =  directions = ['north', 'south', 'east'] verbs = ['go', 'kill', 'eat'] stops = ['the', 'in', 'of'] nouns = ['bear', 'princess'] split_line = sentence.split() for word in split_line.copy(): try: if int(word): rebuild.append(("number", int(word))) split_line.remove(word) except ValueError: pass for word in split_line: if word in directions: rebuild.append(("direction", word)) elif word in verbs: rebuild.append(("verb", word)) elif word in stops: rebuild.append(("stop", word)) elif word in nouns: rebuild.append(("noun", word)) else: rebuild.append(("error", word)) return rebuild