# [Tutor] Help finishing a function

Alan Gauld alan.gauld at yahoo.co.uk
Wed May 17 11:18:36 EDT 2017

```On 17/05/17 14:31, Grace Sanford wrote:
> I am wondering if someone can help/advise me on finishing the code for this
> function:

Please only send one email for a given i8ssue it gets confusing
the same question.

for you so you need to tell us specifically where you need help.
The assignment gives you lots of hints about what to do.
Which bits are you struggling with?

That having been said, there is much I don't understand
myself, see below...

> the_board = [ "_", "_", "_",
>               "_", "_", "_",
>               "_", "_", "_"]
>
> def do_user_move(board, x, y):
>     """
>     signature: list(str), int, int -> bool
>     Given a list representing the state of the board
>     and an x,y screen coordinate pair indicating where
>     the user clicked, update the board
>     with an O in the corresponding position.

The board above is a list of strings and not represented
on any kind of "screen"? I assume there is some other
code somewhere that creates a grid on screen that the
user then clicks on and you need to map the cell to
the string table?

>     The function returns a bool indicated if
>     the operation was successful: if the user
>     clicks on a position that is already occupied
>     or outside of the board area, the move is
>     invalid, and the function should return False,
>     otherise True.
>     """
>     print("user clicked at "+str(x)+","+str(y))
>     width = turtle.window_width ()
>     height = turtle.window_height ()
>     #Given coordinates of user click, update board with "O" in
> corresponding position
>     if x<(-width/2)+(width/3):
>         column = 0
>     elif x>(-width/2)+(width/3) and x<(width/2)-(width/3):
>         column = 1
>     elif x>(width/2)-(width/3):
>         column = 2

This  seems like a complicated way to get the cell!

>     if y>(height/2)-(height/3):
>         row = 0
>     elif y<(height/2)-(height/3) and y>(-height/2)+(height/3):
>         row = 1
>     elif y<(-height/2)+(height/3):
>         row = 2
>     p = row * 3 + column

>     for board[p]=="_":
>     pass #code here

This is not valid Python code. The for loop requires
a sequence of some kind (to be technical an iterable).
This is a boolean test so you should get an error.

>     #Check if user clicks on a position that is already occupied
>     pass #code here

Assuming you get the code above to work and you have a
row/column [pair do you know how to map that onto your
9 element string list? If so put the code here.

>     #Check if user clicks outside the board area
>     if x<(-width/2) or x>(width/2) or y<(-height/2) or y>(height/2):
>         return False

I'd have thought thus test should be right at the top
before attempting all the other stuff!

So in conclusion it looks like we only have half the story
(where is the missing screen?) and the code as provided has
at least one bug (the for loop). But otherwise the task
is fairly clear, so what bit are you stuck with?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

```