Ok you have to work on this more. It looks like you did about 20% of what you need to solve it. Let’s take a look:
def scan(sentence):
# go through each word in the sentence
words = sentence.split()
for word in words:
# try to convert it to a number
try:
int(word)
# if that fails, then lower case it and see if it's in the dictionary
except:
if word.lower() in lexicon:
# if it is then add it to the result
return results
# if not then add an error
else:
print("error")
Where are you making the results
variable? It’s not in there at all. Why do you have int(word) and it’s not assigned to anything? How are you storing the results? Why are you doing return inside your for-loop, which exits the whole function immediately? Where’s your print() debugging statements showing what you tried to log to see how this is working?
If you work on this more and do more gradual debugging you’d figure this out. It seems though that you have a couple misconceptions, and you tend to stop at the first problem and go ask for help instead of trying to solve it.
In your previous code you also had this:
def scan():
results = []
for word in words:
# lots of code
return results
Do you see the error there? Here, what if I did this:
def scan():
results = []
for word in words:
# lots of code
return results
Your code runs the for-loop, processes the first word, and since return results
is under the for-loop (meaning, it’s indented 4 chars further inside the for-loop), then that gets run as part of the for-loop, not after. That causes your for-loop to run once, and return one thing.
My code has the return on the same level as the for-loop and so the whole for-loop runs, then it returns the results one time.
Let’s look at your new code for another thing:
Why do you have this:
def scan(sentence):
for word in words:
#### code cut
if word.lower() in lexicon:
return results # <-------- why?
else:
print("error")
Again, you have a return inside your for-loop, so return exits the whole function which means it jumps out of the for-loop and out of the function and returns that value. It seems you think return does something different, so I’m curious what you think it does. It could be you just have a simple misconception of return, and because of this you keep putting return inside a for-loop and wondering why it doesn’t run. Figure out what your error is regarding return and your for-loop gets fixed.
Also, you’re probably staring at code trying to fix it. That’s not going to fix it though because you have log what your code is doing and watch it run, or run it in a debugger. Try this:
def scan(sentence):
# go through each word in the sentence
words = sentence.split()
for word in words:
print(">>>> top of loop", word)
try:
int(word)
except:
print(">>>> Not an integer.")
if word.lower() in lexicon:
print(">>>> In the lexicon")
# if it is then add it to the result
return results
# if not then add an error
else:
print("error")
If you did that, you’d see the error pretty quick. You’d run it a bunch, see what’s going in, wonder why it keeps stopping, then realize it’s the return (hopefully).