How to add and subtract values of an argument using a dictionary
Ok so I'm very new to python and coding in general so I think I need some
help on this one. I will be frank, in that this is an assignment I have at
the moment in which I am struggling greatly with, due to the fact that I
am new to coding and all that jazz.
So the problem is I have to make this function as follows:
get_position_in_direction(position, direction) that takes a row, column
pair representing the position of a square, and a direction character (one
of n, s, e, w) and returns the position of the adjacent square in the
given direction. For example :
>>> get_position_in_direction((2,3), 'e')
(2, 4)
>>> get_position_in_direction((2,3), 's')
(3, 3)
If you're confused with this, picture it as coordinates on a square (3,3)
and I want to move a direction n, s, e or w (I guess north, east, south or
west) so with the input of which direction the coordinate will change from
(3,3) to (2,3) or whatever.
The way I wanted to do this was using a dictionary such as the one below:
DIRECTIONS = {'n': (-1, 0), 's': (1, 0), 'e': (0, 1), 'w': (0, -1)} I
figured this would be the simplest way of returning the values I wanted.
So yeah like I said I am new to coding and am looking for help. I do
understand a lot in python so please don't feel like you are just doing my
assignment for me and I'll take your answer and just not learn anything,
but I would greatly appreciate some help here. Thanks.
No comments:
Post a Comment