Hey i'm trying to place a ship onto my grid but my translation function is trying to iterate over None type, if someone would write a simple horizontal ship placement in translation function i'm sure i can finish the rest, thank you!

class start(object):
    def __init__(self):
        
            self.GameT = int(raw_input("One Player (1) or Two Player (2): "))
            self.Size = int(raw_input("What are the demenions of this game? (n X n): "))
            if self.Size < 2:
                self.Size = 10
            self.Air = int(raw_input("How many Aircraft Carriers? (Size 5): "))
            self.Bat = int(raw_input("How many Battleships? (Size 4): "))
            self.Sub = int(raw_input("How many Submarines? (Size 3): "))
            self.Cru = int(raw_input("How many Cruisers? (Size 3): "))
            self.Des = int(raw_input("How many Destroyers? (Size 2): "))
        
class board(object):
    def __init__(self):
        pass
    
    def gen(self, size):
        
        board = [[0] * size] * size
        return board


class player(object):
    
    def __init__(self, board, ships):
        shipcount = 0
        for key in ships:
            shipcount += ships[key]
        
        self.ShipPlacement = {}
        pass
    
def determine_hit(x, y, player):
    
    if player[x][y-1] == 0:
        player[x][y-1] = "M"
        return "MISS!"
    
    elif player[x][y-1] == 1:
        player[x][y-1] = "H"
        return "HIT!"
    
    else:
        return "Invalid"

def draw(board):

    for row in board:
        for column in row:
            print column,
        print ""    

def translate(sc, sr, ec, er, board, size):
    #************************************Horizontal********
    if sr == er:
        if sc < ec:
            if ec - sc == size:
                count = -1
                print board[sc + count][sr - 1]
                print board[sc + count][sr - 1] + 1
                print board[sc + count][sr - 1] == 0
                while count < size - 1:
                    if board[sc + count][sr - 1] == 0:
                        board[sc + count][sr - 1] = 5
                        print board
                        
                        print sc + count, "KK"
                    print count
                    count += 1

if __name__ == "__main__":
    settings = start()
    player_board = board()
    ships = {}
    ships["Air"] = settings.Air
    ships["Bat"] = settings.Bat
    ships["Sub"] = settings.Sub
    ships["Cru"] = settings.Cru
    ships["Des"] = settings.Des
    
    
    
    if settings.GameT == 1:
        ships
        p1board = player_board.gen(settings.Size)
        print p1board[1][1]
        human = player(p1board, ships)
        cpuboard = player_board.gen(settings.Size)
        cpu = player(cpuboard, ships)
        
        #********************************Graphic representation***
        
        draw(p1board)
            
            
    #*************************************Ship Placement (attempt)*******
            
        if settings.Air >= 1:
                
            count = 1
                
            while count <= settings.Air:
                
                start_c = int(raw_input("Starting column for your Aircraft Carrier (Size: 5): "))
                start_r = int(raw_input("Starting row for your Aircraft Carrier (Size: 5): "))
                end_c = int(raw_input("Ending column for your Aircraft Carrier (Size: 5): "))
                end_r = int(raw_input("Ending row for your Aircraft Carrier (Size: 5): "))
                print p1board[1][1]
                print end_c - start_c
                print translate(start_c, start_r, end_c, end_r, p1board, 5)
                print "here: "
                draw(p1board)
                count += 1
                
        
        
    #************************************2 players***************   
    #if settings.GameT == 2:
     #   player1 = player_board.gen(settings.Size)
      #  player2 = player_board.gen(settings.Size)
       # print player1
        #print player2
        
        prompt_exit = raw_input("Hit enter to exit")

"board" contains references to the same list, not separate lists. The following code lists the id's which are the same. One item in one list in then changed. The print statement shows that all lists reflect the change. To generate one row, see the last line and then use a for() loop to generate multiple rows.

size=5
board = [[0] * size] * size
print board
for row in board:
    print id(row)
board[2][1]="X"
print board

row=[0 for ctr in range(size)]

my translation function is trying to iterate over None type

Include the full error message. No on wants to sift through 125 lines of questionable code to try and find it.

Edited 4 Years Ago by woooee: n/a

okay so replace "print" on line 111 with "p1board ="
input:
1
1
1
1
1
1
1
1
1
6
1

output error:

Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 113, in <module>
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 49, in draw
TypeError: 'NoneType' object is not iterable

"board" contains references to the same list, not separate lists. The following code lists the id's which are the same. One item in one list in then changed. The print statement shows that all lists reflect the change. To generate one row, see the last line and then use a for() loop to generate multiple rows.

size=5
board = [[0] * size] * size
print board
for row in board:
    print id(row)
board[2][1]="X"
print board

row=[0 for ctr in range(size)]

Include the full error message. No on wants to sift through 125 lines of questionable code to try and find it.

I understand what you've said here, but how would see element at column 3 row 3???????? i don't understand how to use what u have said, some examples would be nice

Include the full error message. No on wants to sift through 125 lines of questionable code to try and find it.

just saying include full error message was enough................ beg my pard.

This article has been dead for over six months. Start a new discussion instead.