Oct-29-2020, 05:56 AM
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
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