14/19 first input being assigned to second input


#1

I am at the testing phase, and the output panel asks for the row guess.
After inputting the row guess, the program assigns that guess to both Row AND Column guess.

This also happens for unrelated code like:
age = int(raw_input("How old are you? "))
girlfriends = int(raw_input("How many girlfriends have you had? "))
print “You are %i years old and have had %i girlfirends?” % (age, girlfriends)
if age < 18 and girlfriends > 5:
print “You stud.”

If I enter age to be 13, the program takes 13 for both variables.

The following code, you may notice, is formatted a little differently so I could test it in Spyder (3.6), where this issue does not occur:

###CODE START ###
from random import randint

board = []

for x in range(0, 5):
  board.append(["O"] * 5)

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

print_board(board)

def random_row(board):
  return randint(0, len(board) - 1)

def random_col(board):
  return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print(ship_row)
print(ship_col)
guess_row = int(input("Guess Row: "))
print (guess_row)
guess_col = int(input("Guess Col: "))
print (guess_col)

if guess_col == ship_col and guess_row == ship_row:
  print ("Congratulations! You sank my battleship!")   
else:
  if guess_row not in range(5) or \
    guess_col not in range(5):
    print ("Oops, that's not even in the ocean.")
  elif (board[guess_row][guess_col] == 'X'):
    print ("You guessed that one already.")
  else:
    print ("You missed my battleship!")
    board[guess_row][guess_col] = "X"
  print_board(board)

###CODE END ###

Note: Codecademy will let me proceed, marking the code correct, despite this error.

Am I missing something here?


#2

Haven’t run your code yet but been considering this segment…

board = []

for x in range(0, 5):
board.append([“O”] * 5)

which is refactored to the following:

>>> def board_template(r, c):
	return [['O'] * c] * r

>>> board = board_template(5,5)
>>> def print_board(board):
    print '\n'.join([" ".join(row) for row in board])

    
>>> print_board(board)
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
>>> 

Often times we discover ways to improve code while we seek out a bug. The time to make that improvement is right at this stage, regardless that the bug might still exist. Turn fat into muscle at every stage of debugging.

Improvement is not just about flow and benchmarking, but about dynamics and re-usability of code. One-off statements do not resonate. Templates do.

>>> print_board(board_template(10,20))
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
O O O O O O O O O O O O O O O O O O O O
>>> 
from random import randrange

def random_row(board):
    return randrange(len(board))

def random_col(board):
    return randrange(len(board[0]))

#3

That’s an interesting way to format the list of lists, but I run into trouble trying to edit individual Os, since board[1][1] edits the entry at index (column) [1] for each row, instead of for row 1 only.
Example:

###INPUT###
board[1][1] = "X"
print_board(board)
###OUTPUT###
O X O O O
O X O O O
O X O O O
O X O O O
O X O O O
# instead of intended:
O O O O O
O X O O O
O O O O O
O O O O O
O O O O O

#4

Yes, I discovered my folly and am trying to work out a solution… Sorry for the confusion.


def board_template(r, c):
	return [['O'] * c for x in range(r)]
>>> 
Guess Row: 4
Guess Col: 4
You missed my battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O X
>>> 

https://repl.it/@mtf/battleshipnolimit


#5

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