Python IF语句没有正确识别价值

时间:2017-03-10 00:34:37

标签: python

我知道这最终必须是格式化问题,但无法弄明白。以下是我的列表的示例:

[['a1', 'a2', 'a3', 'a4', 'a5'], ['b1', 'b2', 'b3', 'b4'], ['c1', 'c2', 'c3'], ['d1', 'd2', 'd3'], ['e1', 'e2']]

我的IF语句代码如下:

guesses = 0
    while guesses < 5:
        guess = input("Guess a spot on the grid (example A10:")
        guesses + 1
        if guess in grid1.board:
            print("Sweet nice hit!")
        else:
            print("sorry try again")

每一个看似正确的条目都是给我一个&#34;抱歉再试一次&#34;错误。知道我的问题可能是什么吗?

编辑在这里添加了更多代码---这是我运行游戏(战舰)的地方

if __name__ == '__main__':
    grid = [['O']*BOARD_SIZE for _ in range(BOARD_SIZE)]
    player1 = Player()
    player2 = Player()
    grid1 = Board(grid)
    grid2 = Board(grid)
    grid1.print_board()
    chris =[]

    for ship_name, ship_size in SHIP_INFO:
        ship1 = Ship(player1,ship_name,ship_size,grid1) #create ship instance
        # ask_coords = ship1.ask_ship_coords(ship_name) #ask user for starting coordinate for ship in form "A1"
        x,y = ship1.split_coordinates(ship_name) #split coordinate from above into x, y variables and check if valid
        direction = ship1.ask_ship_location() # ask for ship's postion --horizontal or vertical
        created_coords = ship1.create_ship_coordinates(x, y, ship_size, direction,grid) # create all coordinates for ship based on size of ship and locatio
        #add coordinates to player's grid
        grid1.board.append(created_coords)

        grid1.print_ship_coordinates(created_coords, direction,grid) #loop through coords for ship to print out on displayed grid

和我的董事会成员:

BOARD_SIZE = 10

class Board:
    board = []

    def __init__(self, grid):
        self.grid = [['O']*BOARD_SIZE for _ in range(BOARD_SIZE)]

0 个答案:

没有答案