I’m working on a program where the user inputs 3 values (one of which is in dictionary notation form).. but I’m having trouble finding out how to work with this special notation.
The user input will look like this:
{'X':'X+YF','Y':'FX-Y'}
which I store in a variable p. I know that with p.keys() I get ['X', 'Y'] and with p.values() I get ['X+YF', 'FX-Y'].
How can I relate 'X' to 'X+YF' to say, if the value of the first key in p is 'X', store 'X+YF' in a var, and if the value of the second key in p is 'Y', store 'FX-Y' in a var?
Is something like this also possible with the same approach stated in the answers below?
If x is found in some string :
swap out the X with the value p['X']
Are you asking how to get the value associated to a particular key? You can acess a value by putting its key in square brackets:
output:
If you want to have different behavior based on which keys exist in the dict, you can use
in:Edit in response to OP’s edit:
My psychic debugging powers tells me that you’re trying to implement an L System. You need to replace all instances of ‘X’ with ‘X+YF’, and all instances of ‘Y’ with ‘FX-Y’. I would implement the function like this: