TypeError: sequence item 0: expected string, NoneType found

后端 未结 3 1018
小鲜肉
小鲜肉 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:21

    If you've arrived here because you were looking for the root cause of "TypeError: sequence item 0: expected string, NoneType found", it can come from doing something along these lines...

    ','.join([None])
    
    0 讨论(0)
  • 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 <function Deploy_Destroyer_1 at 0x1c2634> 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)
    
    0 讨论(0)
  • 2021-02-18 13:36

    Your DisplayChar function does not have a default value. This would do no harm if you'd be handling all possible cases for x, but apparently you're not. Try

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

    but this will probably yield blank strings where you don't expect them.

    Generally, I'd recommend going through a good Python tutorial first. All of your above code can be greatly simplified.

    0 讨论(0)
提交回复
热议问题