Python Forum
Why does my function return None?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does my function return None?
#1
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
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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)"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 562 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,176 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,131 Feb-04-2023, 12:30 PM
Last Post: caslor
  return vs. print in nested function example Mark17 4 1,674 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 4,321 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Get return value from a threaded function Reverend_Jim 3 16,809 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  Return not exiting function?? rudihammad 3 5,192 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  how to keep a Popen instance existant in a function return? Skaperen 7 3,080 Sep-17-2020, 07:10 PM
Last Post: Skaperen
  Function will not return variable that I think is defined Oldman45 6 3,435 Aug-18-2020, 08:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020