In python, how would I go about linking user input to a index in a list? -


i create binary puzzle python. @ moment made 6x6, 8x8 , 10x10 layout shown based on difficulty players wishes play. purpose of puzzle can compared game of sudoku, want input either 0 or 1 on given location player. below find have layout.

if graad  == 1:     easy = [['a', 'b', 'c', 'd', 'e'],      ['_','_','_','_','_','_','_'],      [0,1,0,1,0,1,' |1'],      [1,0,1,0,1,0,' |2'],      [0,1,0,1,0,1,' |3'],      [1,0,1,0,1,0,' |4'],      [0,1,0,1,0,1,' |5'],      [1,0,1,0,1,0,' |6']]     = 0     while < len(easy):         j = 0         s = ""         while j < len(easy[i]):             s = s + str(easy[i][j]) + " "             j = j + 1         print (s)         = + 1 

now problem facing is, how can let python know when player fills in position 3 on column c , row 5 0 example? thinking of if statement checks input on either a, b, c d, e... row 1,2,3,4,5.. going lot of if statements.

edit1: ok clarify.i wanted post picture need more posts. example, have game board of 6x6 cells. of them filled 1 , of them filled 0 , of them empty because goal have in end layout in python code.(that's solution). want user fill in empty cells. now, let's player wants fill in a-1 1, how python know input a-1 linked index [0][0] in list?

a simple way convert letter indices numbers use ord() function, returns numerical code of single character. since using upper-case letters, 'a' being label column index 0, can do

column = ord(letter) - ord('a') 

that convert 'a' 0, 'b' 1, etc.


here's short example program vaguely based on code on question.

it accepts moves in form a10 set location a1 '1', 'b30' set location b3 '0'. accepts lower case letters, too, 'd11' same 'd11'. hit ctrl-c exit.

tested on python 2.6.6, should work correctly on python 3. (to run on python 2, change input() raw_input()).

#! /usr/bin/env python  def print_grid(g):     gsize = len(g)     base = ord('a')     print(' '.join([chr(base + i) in range(gsize)]))     print((gsize * 2) * '-')     i, row in enumerate(g, 1):         print(' '.join(row) + ' | ' + str(i))     print('\n')   def main():     gsize = 9     rowstr = gsize * '_'     grid = [list(rowstr) in range(gsize)]     print_grid(grid)      while true:         move = input('enter move: ')         letter, number, bit = move.strip()         col = ord(letter.upper()) - ord('a')         row = int(number) - 1          grid[row][col] = bit         print_grid(grid)   if __name__ == "__main__":     main() 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -