TypeError: sequence item 0: expected string, NoneType found

后端 未结 3 1020
小鲜肉
小鲜肉 2021-02-18 12:43

I\'m trying to improve a game of battleships. The original version works fine with no errors. I have written code to help overcome the fact that the first version places the shi

3条回答
  •  感动是毒
    2021-02-18 13:33

    The problem is most likely somewhere within these 4 lines:

    for x in range(6):
        print letters[x],"  ".join(map(DisplayChar,Player[x]))," "*18,"| ","  ".join(map(DisplayChar,Opponent[x]))
    for x in range(6,12):
        print letters[x],"  ".join(map(DisplayChar,Player[x]))," | ","  ".join(map(DisplayChar,Opponent[x]))
    

    Within these lines, you've used a join statement multiple times. The join statement requires a list of strings in order to work. However, when you're mapping DisplayChar to Player[x], the DisplayChar function is returning the value None instead of a string of some sort.

    If you look at the DisplayChar function, it handles values only from 0 to 4. The lists you use probably include additional numbers or characters. If x happened to be something like 5, DisplayChar will terminate and simply return the value None. Remember, functions return the value None by default.

    You either need to handle these additional numbers within DisplayChar, or modify DisplayChar to contain an else statement to return an empty string, like so:

    def DisplayChar(x):
        if x==0: 
            return '?'
        elif x==1:
            return ' '
        elif x==2:
            return 'X'
        elif x==3:
            return ' '
        elif x==4:
            return '*'
        else:
            return ' '
    

    Edit:

    Ok, I think I might know what's going on, given the new edits.

    Notice when you were printing out Player[x], it printed the second time around?

    That means somewhere, buried deep inside your code, you've done something to the effect of Player[row] = Deploy_Destroyer_1 (notice the missing parenthesis!). Instead of calling the function, you've assigned the function.

    Hunting for, and adding the missing parenthesis should most likely solve the problem.

    Edit 2:

    I think your problem is with this line: Player = DeployFleet(Player), Deploy_Destroyer_1(Player)

    If you try doing a print Player immediately after, I think you'll most likely see a large list of numbers, followed by None.

    This is because the DeployFleet function is returning the table (I think?) whereas the Deploy_Destroyer_1 function returns nothing. Instead, it just mutates the Player table.

    To fix this, try doing either this:

    Player = DeployFleet(Player)
    Deploy_Destroyer_1(Player)
    

    ...or modify Deployer_Destroyer_1 so that it returns Player when it's finished, so you can do this:

    Player = DeployFleet(Player)
    Deploy_Destroyer_1(Player)
    

提交回复
热议问题