Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrigette Eckert
16,957 Pointsmove_player function input format.
I am trying to write a test for move player. If I pass in a tuple for player and a string for move I get an error: x, y = player['location'] TypeError: tuple indices must be integers or slices, not str
> >> from dd_game import move_player
>>> move_player((1,2),
... "LEFT")
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/brigette/Code/treehouse-projects/python/testing/1_first_steps_testing/dd_game.py", line 84, in move_player
x, y = player['location']
TypeError: tuple indices must be integers or slices, not str
What does my input need to look like for this function?
2 Answers
James J. McCombie
Python Web Development Techdegree Graduate 21,199 PointsHi,
you should be writing the dockets inside the docstring for the function 'move_player'; so you should not have to import it.
I had to dig through the lessons, but is this the function you are writing a doctest for?
def move_player(player, move):
x, y = player
if move == "LEFT":
x -= 1
if move == "RIGHT":
x += 1
if move == "UP":
y -= 1
if move == "DOWN":
y += 1
return x, y
if so to test this I would do
def move_player(player, move):
"""
>>> move_player((1, 1), 'LEFT')
(0, 1)
"""
# rest of function code...
Brigette Eckert
16,957 PointsIt was the see if you can write your own doctests part of the lesson, just trying to figure out how to format the arguments when I call the move_player() function. The ((x, y), 'Direction' ) gave me the error before.
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Pointscan you show the code for move_player, the one you are using, and the doctest you have written?
James J. McCombie
Python Web Development Techdegree Graduate 21,199 PointsJames J. McCombie
Python Web Development Techdegree Graduate 21,199 PointsThe instructions in the doctest I think of as being what you were entering into a python shell, as you would do if you were playing with your code to see if it works.
So the first line is just calling the function with some arguments, and since you know what to expect, the second line is just the repl output you would see in the terminal