Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列表对象引用检查器python_Python_List - Fatal编程技术网

列表对象引用检查器python

列表对象引用检查器python,python,list,Python,List,我在做一个小项目,在编程中遇到了一个小错误。这是一个基本的战舰游戏,到目前为止,我已经设置了两个“战舰”,当我或敌人双方都被击中时,游戏就结束了 def enemy_board(): global enemy_grid enemy_grid = [] for i in range (0,10): enemy_grid.append(["="] * 10) def random_row_one(enemy_grid): return randint(0, len(enemy_gri

我在做一个小项目,在编程中遇到了一个小错误。这是一个基本的战舰游戏,到目前为止,我已经设置了两个“战舰”,当我或敌人双方都被击中时,游戏就结束了

def enemy_board():
global enemy_grid
enemy_grid = []
for i in range (0,10):
    enemy_grid.append(["="] * 10)
def random_row_one(enemy_grid):
    return randint(0, len(enemy_grid) - 1)
def random_col_one(enemy_grid):
    return randint(0, len(enemy_grid) - 1)   
def random_row_two(enemy_grid):
    return randint(0, len(enemy_grid) - 1)
def random_col_two(enemy_grid):
    return randint(0, len(enemy_grid) - 1)
global x_one
x_one = random_row_one(enemy_grid)
global y_one
y_one = random_col_one(enemy_grid)
global x_two
x_two = random_row_two(enemy_grid)
global y_two
y_two = random_col_two(enemy_grid)
print(x_one)
print(y_one)
print(x_two)
print(y_two)
这是我列表的基础,但在代码的后面,它给了我一点麻烦

elif enemy_grid.count("H") == 2:
    print("\nYou got them all!\n")
    break
更新 对不起,我有点不明白我的意思

def my_board():
   global my_grid
    my_grid = []
    for i in range (0,10):
        my_grid.append(["O"] * 10)
        def my_row_one(my_grid):
        int(input("Where do you wish to position your first ship on the x-axis? "))
    def my_col_one(my_grid):
        int(input("Where do you wish to position your first ship on the y-axis? "))
    global x_mio
    x_mio = my_row_one(my_grid)
    global y_mio
    y_mio = my_col_one(my_grid)
    def my_row_two(my_grid):
        int(input("\nWhere do you wish to position your other ship on the x-axis? "))
    def my_col_two(my_grid):
    int(input("Where do you wish to position your other ship on the y-axis? "))
    global x_mit
    x_mit = my_row_two(my_grid)
    global y_mit
    y_mit = my_col_two(my_grid)   
def enemy_board():
    global enemy_grid
    enemy_grid = []
    for i in range (0,10):
        enemy_grid.append(["="] * 10)
    def random_row_one(enemy_grid):
        return randint(0, len(enemy_grid) - 1)
    def random_col_one(enemy_grid):
        return randint(0, len(enemy_grid) - 1)   
    def random_row_two(enemy_grid):
        return randint(0, len(enemy_grid) - 1)
    def random_col_two(enemy_grid):
        return randint(0, len(enemy_grid) - 1)
    global x_one
    x_one = random_row_one(enemy_grid)
    global y_one
    y_one = random_col_one(enemy_grid)
    global x_two
    x_two = random_row_two(enemy_grid)
    global y_two
    y_two = random_col_two(enemy_grid)
    print(x_one)
    print(y_one)
    print(x_two)
    print(y_two)
title()
my_board()
enemy_board()
m = 20
guesses = m
while guesses > 0:      
    def printmi_board(my_grid):
        for row in my_grid:
            print(" ".join(row))
    def printyu_board(enemy_grid):
        for row in enemy_grid:
            print (" ".join(row))
    print(printmi_board(my_grid))
    print(printyu_board(enemy_grid))
    try:
        guess_x = int(input("Take aim at the x-xalue: "))
    except ValueError:
        print("\nI SAID TAKE AIM!\n")
        guess_x = int(input("Take aim at the x-xalue: "))
    try:
        guess_y = int(input("Take aim at the y-value: "))
    except ValueError:
        print("\nDo you have wax in your ears?? AIM!\n")
        guess_y = int(input("Take aim at the y-value: "))
    comp_x = randint(0, len(my_grid) - 1)
    comp_y = randint(0, len(my_grid) - 1)
    if x_one == guess_x and y_one == guess_y:
        print("\nYou hit one! \n")
        enemy_grid[guess_x - 1][guess_y - 1] = "H"
        continue
    elif x_two == guess_x and y_two == guess_y:
        enemy_grid[guess_x - 1][guess_y - 1] = "H"
        print("\nYou hit one! \n")
        continue
    elif enemy_grid[guess_x - 1][guess_y - 1] == "O":
        print("\nYou've tried there before! Here's another round.\n")
        print("You have " + str(guesses) + " rounds left, cadet.\n\n")
        continue
    elif enemy_grid.count("H") == 2:
        print("\nYou got them all!\n")
        break
    else:
        if guess_x not in range(10) or guess_y not in range(10):
            print("\nThat's not even in the OCEAN!! Take another free round then.\n")
            print("You have " + str(guesses) + " rounds left, cadet.\n\n")
            continue
        elif enemy_grid[guess_x][guess_y] == "O":
            print("\nYou've tried there before! Here's another round.\n")
            print("You have " + str(guesses) + " rounds left, cadet.\n\n")
            continue
        else:
            print("\nYou missed, soldier!\n")
            guesses = guesses - 1
            print("You have " + str(guesses) + " rounds left, cadet.\n\n")
            enemy_grid[guess_x - 1][guess_y - 1] = "O"
            if comp_x == x_mio and comp_y == y_mio:
                my_grid[comp_x - 1][comp_y - 1] = "H"
                print("\nThe enemy hit you! \n")
                continue
            elif comp_x == x_mit and comp_y == y_mit:
                my_grid[comp_x - 1][comp_y - 1] = "H"
                print("\nThe enemy hit you! \n")
                continue
            elif my_grid.count("H") == 2:
                print("We have to retreat! They've sunken all of your ships...")
                break
            else:
                my_grid[comp_x - 1][comp_y - 1] = "="
                continue

如果有什么不同的话,我使用的是Python3。因此,如果玩家在网格上点击了正确的位置,那么它将显示为“H”,而不是“=”或“O”。所以我只是想知道我是否可以计算用来结束if循环的那些“H”。

您还没有真正解释这个问题,而且缺少太多的代码,所以很难告诉您出了什么问题,不过我还是要猜测一下

我的猜测是,你创建一个“=”网格来表示玩家的棋盘,然后如果他们的船被“击中”,你就用一个“H”替换该位置的“=”

您创建的结构(敌方网格)看起来像:

[[====]
 [====]
 [====]
 [....]]
在这种情况下,您的测试,
敌方网格。计数(“H”)
没有意义,因为敌方网格是一个包含其他列表的列表(因此Hs的计数将始终为0-它们在列表的第二层更深)

您可能需要一个更符合以下内容的测试:

[cell for row in enemy_grid for cell in row].count('H')

问题是什么?破损的缩进使代码很难理解<代码>全局函数外的声明无效。您不需要用不同的名称定义同一函数四次(
random_u2;(row/col)2;(one/two)
)!只定义一次,然后叫四次。“给我一点麻烦”什么麻烦?请显示完整的错误消息。很抱歉,还有一次更新,为了最小化问题中的空间,我删除了一个定义(title())。如果是这样的话,很抱歉,谢谢!我只是在重新考虑我的代码,你的建议很正确!很抱歉,我无法更轻松地表达我的问题,但你明白了。我只需要在代码中切换IF语句的位置,根据您的建议,它现在可以完美地工作了!非常感谢。