Python IF语句未正确标识值

2024-09-28 22:23:30 发布

您现在位置: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")

每一个看起来应该是正确的条目都会给我一个“对不起,再试一次”的错误。知道我的问题是什么吗?你知道吗

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

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

Tags: nameinboardforsizecoordsgridask