Battleship - Extra Credit


#1

This code says that the r is invalid, why is that and how do I fix that?

name = input('Player One, what is your name? ')
name_2 = input('Player Two, what is your name? ')
from random import randint
board = []
for x in range(5):
    board.append(["O"] * 5)
def print_board(board):
    for row in board:
        print(" ".join(row))
print('This is the turn of %s' % (name)) 
print_board(board)
x = int(input('Where do you want your first city (row)? '))
a = int(input('Where do you want your first city (collum)? '))
board[x][a] = 'C'
print_board(board)
q = int(input('Where do you want your troops (row)? '))
w = int(input('Where do you want your troops (collum)? '))
board[q][w] = 'T'
print('This is the turn of %s' % (name_2))
print_board(board)
x_2 = int(input('Where do you want your first city (row)? '))
a_2 = int(input('Where do you want your first city (collum)? '))
board[x_2][a_2] = 'C'
print_board(board)
q_2 = int(input('Where do you want your troops (row)? '))
w_2 = int(input('Where do you want your troops (collum)? '))
board[q_2][w_2] = 'T'
print_board(board)
for turn in range(15):
    first = input('%s, do you want to move your troops (yes or no)?' % (name))
    if first == 'yes':
        o = int(input('Where would you like to move them (row)? ')
        r = int(input('Where would you like to move them (collum)? ')
        board[x][a] = 'C'
        board[o][r] = 'T'
        board[x_2][a_2] = 'C'
        board[q_2][w_2] = 'T'
        board[q][w] = 'O'
    else:
        break

#2

You have quite a bit of code... What does it say is wrong with the r variable?


#3

invalid syntax, the o worked, at least, I think it did


#4

What specifically does it say is wrong with the Syntax?


#5

It just says invalid syntax, that's it. Is it something with the format?


#6

I just fixed a few of the problems. But, now it says, "Traceback (most recent call last):
File "C:\Users\Home\AppData\Local\Programs\Python\Python35-32\Civ.py", line 35, in
board[o][r] = 'T'
TypeError: list indices must be integers or slices, not str"

What would you recommend I do. Here is the new code:

name = input('Player One, what is your name? ')
name_2 = input('Player Two, what is your name? ')
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print(" ".join(row))
print('This is the turn of %s' % (name))
print_board(board)
x = int(input('Where do you want your first city (row)? '))
a = int(input('Where do you want your first city (collum)? '))
board[x][a] = 'C'
print_board(board)
q = int(input('Where do you want your troops (row)? '))
w = int(input('Where do you want your troops (collum)? '))
board[q][w] = 'T'
print('This is the turn of %s' % (name_2))
print_board(board)
x_2 = int(input('Where do you want your first city (row)? '))
a_2 = int(input('Where do you want your first city (collum)? '))
board[x_2][a_2] = 'C'
print_board(board)
q_2 = int(input('Where do you want your troops (row)? '))
w_2 = int(input('Where do you want your troops (collum)? '))
board[q_2][w_2] = 'T'
print_board(board)
for turn in range(15):
first = input('%s, do you want to move your troops (yes or no)?' % (name))
if first == 'yes':
o = input('Where would you like to move them? (Row, remember, you can only move one space!) ')
r = input('Where would you like to move them? (Collum, remember, you can only move one space!) ')
board[q][w] = 'O'
board[o][r] = 'T'
print_board(board)
else:
break


#7

I did indent the if statement


#8

Hello? What should I do?


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.