Yes, so the issue is a little more subtle than @florian is explaining. @florian is correct that you are trying to search for 1234 when the user has typed in “1234”. BUT, that’s not the actual problem with the code.
First, imagine you are not handling numbers. Just do words. In that case this would work, and you’d pass those tests. Then, when you type in a word that does not exist what happens? You get something like this on the return:
(None, “thisworddoesntwork”)
Now, if someone types in a number–remembering that we’re talking about your code but in a form that does NOT handle numbers yet–you would get the same error:
(None, “1234”)
You of course then say, “Aha, I just have to handle that number in the test.” You then write into the dictionary this pair:
1234: “number”
But, that’s now how the number comes in from the users. When someone types on the keyboard python doesn’t do anything with it. If they type “1234” then it comes as a string, “1234”. You have to manually convert it to a number using int() then work with that (as @florian said).
HOWERVER, changing your dictionary to this:
“1234”: “number”
Still doesn’t actually work. It might get the test to run, but what if the user types 9837987345875. That’s still a number, but your tiny lexicon only knows “1234”. Are you going to put every number possible into the dictionary? You could. Or, you could use the above information about int() to come up with a permanent future proof solution. Try this:
Python 3.6.8 (default, Aug 20 2019, 17:12:48)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> int("1234")
1234
>>> int("adfasdfasdfasdf")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'adfasdfasdfasdf'
>>> try:
... int("asdfadfasdf")
... except:
... print("not a number")
...
not a number
>>>
You can see that if you run int() it will convert strings full of numbers into Python integers. BUT, if you feed int() something that’s not a number it explodes with an error. If you wrap that with a try/except then you can handle the case where it’s not a number. That means you can use this logic:
- Attempt to convert every input to an integer using int() but inside a
try:
and then if that works you can return it as the (1234, “number”).
- If that explodes, then the
except:
will run, that’s where you put your current ```lexicon.get(word, ‘error’) code.
- Now if someone types any number you get a number, but if that fails then you look it up in the lexicon.
There’s other ways to do this, like using isdigit https://docs.python.org/3/library/stdtypes.html#str.isdigit so try them out.