# Battleship extra credit

#1

I made this code and it is asking where you want to place the ship 2 times and i dont know why.
Please let me know if you know why.
This is the code:

board1 = []
board2 = []

for x in range(10):
board1.append(["O"] * 10)

for x in range(10):
board2.append(["O"] * 10)

def print_board1(board):
for row in board:
print " ".join(row)
def print_board2(board):
for row in board:
print " ".join(row)

print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Board User 2"
print_board2(board2)

print "Let's play Battleship!"
print "Try to destroy all your opponents battleship!"
print"Good luck!"
print " "
print " "

def U1_Input_row1(board1):
x = float(raw_input("User 1, in what row do you want to place your first ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_row1(board1)

def U1_Input_col1(board1):
x = float(raw_input("User 1, in what col do you want to place your first ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_col1(board1)

ship1 = [U1_Input_row1(board1), U1_Input_col1(board1)]

def U1_Input_row2(board1):
x = float(raw_input("User 1, in what row do you want to place your second ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_row2(board1)

def U1_Input_col2(board1):
x = float(raw_input("User 1, in what col do you want to place your second ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_col2(board1)

ship2 = [U1_Input_row2(board1), U1_Input_col2(board1)]

def U1_Input_row3(board1):
x = float(raw_input("User 1, in what row do you want to place your third ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_row3(board1)

def U1_Input_col3(board1):
x = float(raw_input("User 1, in what col do you want to place your third ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_col3(board1)

ship3 = [U1_Input_row3(board1), U1_Input_col3(board1)]

def U1_Input_row4(board1):
x = float(raw_input("User 1, in what row do you want to place your fourth ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_row4(board1)

def U1_Input_col4(board1):
x = float(raw_input("User 1, in what col do you want to place your fourth ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U1_Input_col4(board1)

ship4 = [U1_Input_row4(board1), U1_Input_col4(board1)]

if ship1 == ship2 or ship1 == ship3 or ship1 == ship4 or ship2 == ship3 or ship2 == ship4 or ship3 == ship4:
print "YOU CANT PLACE 2 SHIPS IN THE SAME SPOT"
U1_Input_row1(board1)
U1_Input_col1(board1)
U1_Input_row2(board1)
U1_Input_col2(board1)
U1_Input_row3(board1)
U1_Input_col3(board1)
U1_Input_row4(board1)
U1_Input_col4(board1)

def U2_Input_row1(board2):
x = float(raw_input("User 2, in what row do you want to place your first ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_row1(board2)

def U2_Input_col1(board2):
x = float(raw_input("User 2, in what col do you want to place your first ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_col1(board2)

ship1u2 = [U2_Input_row1(board1), U2_Input_col1(board1)]

def U2_Input_row2(board2):
x = float(raw_input("User 2, in what row do you want to place your second ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_row2(board2)

def U2_Input_col2(board2):
x = float(raw_input("User 2, in what col do you want to place your second ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_col2(board2)

ship2u2 = [U2_Input_row2(board1), U2_Input_col2(board1)]

def U2_Input_row3(board2):
x = float(raw_input("User 2, in what row do you want to place your third ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_row3(board2)

def U2_Input_col3(board2):
x = float(raw_input("User 2, in what col do you want to place your third ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_col3(board2)

ship3u2 = [U2_Input_row3(board1), U2_Input_col3(board1)]

def U2_Input_row4(board2):
x = float(raw_input("User 2, in what row do you want to place your fourth ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_row4(board2)

def U2_Input_col4(board2):
x = float(raw_input("User 2, in what col do you want to place your fourth ship?"))
if x > 0 and x < 11 and x%1 == 0:
return x - 1
else:
print "Please enter an integer and a number between 1 and 10"
U2_Input_col4(board2)

ship4u2 = [U2_Input_row4(board1), U2_Input_col4(board1)]

if ship1u2 == ship2u2 or ship1u2 == ship3u2 or ship1u2 == ship4u2 or ship2u2 == ship3u2 or ship2u2 == ship4u2 or ship3u2 == ship4u2:
print "YOU CANT PLACE 2 SHIPS IN THE SAME SPOT"
U2_Input_row1(board2)
U2_Input_col1(board2)
U2_Input_row2(board2)
U2_Input_col2(board2)
U2_Input_row3(board2)
U2_Input_col3(board2)
U2_Input_row4(board2)
U2_Input_col4(board2)

U1_Input_row1 = U1_Input_row1(board1)
U1_Input_col1 = U1_Input_col1(board1)
U1_Input_row2 = U1_Input_row2(board1)
U1_Input_col2 = U1_Input_col2(board1)
U1_Input_row3 = U1_Input_row3(board1)
U1_Input_col3 = U1_Input_col3(board1)
U1_Input_row4 = U1_Input_row4(board1)
U1_Input_col4 = U1_Input_col4(board1)
U2_Input_row1 = U2_Input_row1(board2)
U2_Input_col1 = U2_Input_col1(board2)
U2_Input_row2 = U2_Input_row2(board2)
U2_Input_col2 = U2_Input_col2(board2)
U2_Input_row3 = U2_Input_row3(board2)
U2_Input_col3 = U2_Input_col3(board2)
U2_Input_row4 = U2_Input_row4(board2)
U2_Input_col4 = U2_Input_col4(board2)

total_ships1 = 4
total_ships2 = 4
U1_Score = 0
U2_Score = 0
for turn in range(10):
U1_Guess_row = int(raw_input("User 1, in what row do you think an enemy ship is?")) - 1
U1_Guess_col = int(raw_input("User 1, in what col do you think an enemy ship is?")) - 1
if U1_Guess_row == U2_Input_row1 and U1_Guess_col == U2_Input_col1 or U1_Guess_row == U2_Input_row2 and U1_Guess_col == U2_Input_col2 or U1_Guess_row == U2_Input_row3 and U1_Guess_col == U2_Input_col3 or U1_Guess_row == U2_Input_row4 and U1_Guess_col == U2_Input_col4:
if board2[U1_Guess_row][U1_Guess_col] == "#":
print "You sunk that ship already"
else:
board2[U1_Guess_row][U1_Guess_col] = "#"
total_ships1 -= 1
U1_Score += 1
print "Score User 1:", U1_Score
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Score User 2:", U2_Score
print "Board User 2"
print_board2(board2)
print "Congratulations! You sunk an enemy battleship"
if total_ships1 == 0:
print "User 1, you won the game!"
print "You destroyed all hostile ships!"
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Board User 2"
print_board2(board2)
break
else:
if (U1_Guess_row < 0 or U1_Guess_row > 9) or (U1_Guess_col < 0 or U1_Guess_col > 9):
print "Oops, that's not even in the ocean."
elif (board2[U1_Guess_row][U1_Guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
if turn == 10:
print "Its a draw"
board2[U1_Guess_row][U1_Guess_col] = "X"
print "Turn", turn + 1
print "Score User 1:", U1_Score
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Score User 2:", U2_Score
print "Board User 2"
print_board2(board2)

``````U2_Guess_row = int(raw_input("User 2, in what row do you think an enemy ship is?")) - 1
U2_Guess_col = int(raw_input("User 2, in what col do you think an enemy ship is?")) - 1
if U2_Guess_row == U1_Input_row1 and U2_Guess_col == U1_Input_col1 or U2_Guess_row == U1_Input_row2 and U2_Guess_col == U1_Input_col2 or U2_Guess_row == U1_Input_row3 and U2_Guess_col == U1_Input_col3 or U2_Guess_row == U1_Input_row4 and U2_Guess_col == U1_Input_col4:
if board1[U2_Guess_row][U2_Guess_col] == "#":
print "You sunk that ship already"
else:
print "Congratulations! You sunk an enemy battleship"
board1[U2_Guess_row][U2_Guess_col] = "#"
total_ships2 -= 1
U2_Score += 1
print "Score User 1:", U1_Score
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Score User 2:", U2_Score
print "Board User 2"
print_board2(board2)
if total_ships2 == 0:
print "User 2, you won the game!"
print "You destroyed all hostile ships!"
break
else:
if (U2_Guess_row < 0 or U2_Guess_row > 9) or (U2_Guess_col < 0 or U2_Guess_col > 9):
print "Oops, that's not even in the ocean."
elif (board1[U2_Guess_row][U2_Guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
if turn == 10:
print "Its a draw"
board1[U2_Guess_row][U2_Guess_col] = "X"
print "Turn", turn + 1
print "Score User 1:", U1_Score
print "Board User 1"
print_board1(board1)
print "----------------------------------------------"
print "Score User 2:", U2_Score
print "Board User 2"
print_board2(board2)``````

#2

Whenever you would like to paste code, since this forum supports Markdown Syntax you can use three backticks before and after your code block like so:

```
def my_func(x):
my_var = 0
if x > my_var:
sum = my_var + x
else:
print "Odelay!"
return sum
```

and it will retain proper indentation and even have nifty syntax highlighting:

``````def my_func(x):
my_var = 0
if x > my_var:
sum = my_var + x
else:
print "Odelay!"
return sum``````

And if you are to paste that much code, you might as well use the lab and paste a link to your lab project instead...