Python Forum
Why does my function return None? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why does my function return None? (/thread-30634.html)



Why does my function return None? - vg100h - Oct-29-2020

Hello Team,

I am trying to build a dictionary that:

- would take in a word
- check if it exists in the dictionary (my_file_data var) with the translate function
- if it is not there already, it will check for some close options to the input word with the word_match function(in case user misspelled the word or to avoid typo)
- it then prints the result

Scenario 1: If I type a word that is in the dictionary, the function "translate" returns the correct value without any error.

Scenario 2: If I am typing a word that is not in the dictionary, the function "translate" will be ran more than once and that's when it will return "None" instead of the desired value, like in the Scenario 1.

I've added numbered print statements to see what is the output at each step and it does not make sense to me why before exiting the function, the my_file_data[word] is correct and when it is returned from the function, it is "None".

Please look at the output below and share your thoughts. There is something that I understood wrong in my Python training and I can't spot the problem.

Scenario 1 OUTPUT:

Word please: rain
no2 rain
no3 rain
no5 ['Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres.', 'To fall from the clouds in drops of water.']
no1 ['Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres.', 'To fall from the clouds in drops of water.']
The word ['rain', 'train', 'rainny'] means:
Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres.
To fall from the clouds in drops of water.

Process finished with exit code 0

Scenario 2 OUTPUT:

Word please: raino
no2 raino
no4 ['rain', 'train', 'rainny']
Your word was not found. You may choose from the following options: rain train rainny
Choose an option or type ESC to exit: rain
no2 rain
Traceback (most recent call last):
File "C:/Users/vg100h/PycharmProjects/pythonProject1/main.py", line 57, in <module>
if len(word_translation) > 0:
TypeError: object of type 'NoneType' has no len()
no3 rain
no5 ['Precipitation in the form of liquid water drops with diameters greater than 0.5 millimetres.', 'To fall from the clouds in drops of water.']
no1 None

Process finished with exit code 1


RE: Why does my function return None? - buran - Oct-29-2020

Please, post your code (minimal reproducible example), incl. sample input data.
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.


RE: Why does my function return None? - bowlofred - Oct-29-2020

Without the code, we're not going to be able to tell anything.

Consider using the debugger and investigating variables as you execute your code.


RE: Why does my function return None? - vg100h - Oct-29-2020

Please close or delete this thread, cause I just saw the mistake. I missed to return the output the second time around --> "return translate(option_input)"