Re: [Tutor] Help finishing a function

2017-05-17 Thread Alan Gauld via Tutor
Please use Reply-All or Reply-List when replying to the list,
otherwise it only goes to me.

On 17/05/17 17:21, Grace Sanford wrote:
> Syntactically speaking, how can I check if an element in the list
> "board" at position p equals "_" and then change that element to "0"?

You can use the == operator:

if board[index] == "_":
board[index] = "O"
else:
   # report an error? return false?


-- 
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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help finishing a function

2017-05-17 Thread Alan Gauld via Tutor
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
when people start responding to two different threads about
the same question.

Also please give us more information. We won;t do your homework
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help finishing a function

2017-05-17 Thread Grace Sanford
I am wondering if someone can help/advise me on finishing the code for this
function:

import turtle
import time
import random

# This list represents the board. It's a list
# of nine strings, each of which is either
# "X", "O", "_", representing, respectively,
# a position occupied by an X, by an O, and
# an unoccupied position. The first three
# elements in the list represent the first row,
# and so on. Initially, all positions are
# unoccupied.
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 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
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
#Check if user clicks on a position that is already occupied
pass #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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor