I am very confused on what return is and what it does. I have read A LOT.
The challenge is to take a string of input from the user and create nested dictionaries. There should be 1 dictionary that has each letter, and for each letter there are 2 key value pairs. How far that letter is from the front and how far it is from the back.
Now this code works although I was told it is creating the dictionary 8 times (I don't understand why, I assumed it would continue to append to the dictionary)
Please tell me the string you want to use: asdf
{'a': {'Numbers from back': 3, 'Numbers from front': 0}}
{'s': {'Numbers from back': 2, 'Numbers from front': 1}}
{'d': {'Numbers from back': 1, 'Numbers from front': 2}}
{'f': {'Numbers from back': 0, 'Numbers from front': 3}}
What I was told was:
01) It is creating 8 dictionaries
02) It doenst return anything (this is where I am super confused.)
03) To test if the problem is solved i should be able to run this statement outside the function and get zero: print(letter_dict['a']['distance_from_first_letter']) (I swapped out the names already. I am calling the dictionary and the key something different).
The moment i add return my code breaks
NameError: name 'numbersdict' is not defined
I also notice the loop dies.
So I guess my question are
01) What does return do?
02) How do I loop through all of this information to create my nested dictionaries?
03) Where does the return go?
Sorry for the long post. I have been at this for 4 hours and I feel retarded.
I thought return would return numbersidct outside the loop so I could use it in the main program but it does not appear so.
The challenge is to take a string of input from the user and create nested dictionaries. There should be 1 dictionary that has each letter, and for each letter there are 2 key value pairs. How far that letter is from the front and how far it is from the back.
Now this code works although I was told it is creating the dictionary 8 times (I don't understand why, I assumed it would continue to append to the dictionary)
def wedothemathz (datstring): whatlen = len(datstring) fromfront = "Numbers from front" fromback = "Numbers from back" for letter in datstring: where = datstring.find(letter) fromback1 = int(whatlen) - int(where) fromback1 = fromback1 - 1 numbersdict = {letter:{fromback:fromback1, fromfront:where}} print (numbersdict) wedothemathz(input ("Please tell me the string you want to use: "))Here is the output:
Please tell me the string you want to use: asdf
{'a': {'Numbers from back': 3, 'Numbers from front': 0}}
{'s': {'Numbers from back': 2, 'Numbers from front': 1}}
{'d': {'Numbers from back': 1, 'Numbers from front': 2}}
{'f': {'Numbers from back': 0, 'Numbers from front': 3}}
What I was told was:
01) It is creating 8 dictionaries
02) It doenst return anything (this is where I am super confused.)
03) To test if the problem is solved i should be able to run this statement outside the function and get zero: print(letter_dict['a']['distance_from_first_letter']) (I swapped out the names already. I am calling the dictionary and the key something different).
The moment i add return my code breaks
def wedothemathz (datstring): whatlen = len(datstring) fromfront = "Numbers from front" fromback = "Numbers from back" for letter in datstring: where = datstring.find(letter) fromback1 = int(whatlen) - int(where) fromback1 = fromback1 - 1 numbersdict = {letter:{fromback:fromback1, fromfront:where}} return (numbersdict) print (numbersdict) wedothemathz(input ("Please tell me the string you want to use: ")) print(numbersdict[a][fromfront])print(numbersdict[a][fromfront])
NameError: name 'numbersdict' is not defined
I also notice the loop dies.
So I guess my question are
01) What does return do?
02) How do I loop through all of this information to create my nested dictionaries?
03) Where does the return go?
Sorry for the long post. I have been at this for 4 hours and I feel retarded.
I thought return would return numbersidct outside the loop so I could use it in the main program but it does not appear so.