TypeError: sequence item 0: expected string, NoneType found

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

提交回复
热议问题