"Int()" Error


#1

https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/test-run?action=lesson_resume&link_content_target=interstitial_undefined


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
0
4
Guess Row: **Traceback (most recent call last):**
**  File "python", line 24, in <module>**
**ValueError: invalid literal for int() with base 10: 'SCT'**

<What do you expect to happen instead?>
Well… not this.

python

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(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == ship_row and guess_col == ship_col:
  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)



#2

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(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == ship_row and guess_col == ship_col:
  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)

#3

well, this bug seems to come back every once in a while, the validation is done using SCT (submission correctness test)

and for some reason the SCT enters itself into raw_input, and not validating the code

we could attempt to bypass the problem by replacing the raw_inputs:

guess_row = 3
guess_col = 3

with just integer values. Sorry for inconvenience, hopes this works. If you run the code on repl.it you will see its working fine


#4

Just as a sidenote, but why do we use raw_input instead of simply “input”?
Does this have to do with something in any way? (saw on another tread a recomandation to use input instead of raw_input).

Edit: changing the input to another number did indeed work.


#5

because in python2, input() evaluates the input as code, which we can prove:

see how the comparison is actually evaluated? So using input() sounds like a bad idea, not sure who suggested this, but i think its a terrible idea.

raw_input takes the input as string, much better idea


#6

Hello all, would you mind giving me some more detail here? Though @stetim94 was able to quickly recognize this error, I’ll need a bit more information if I am to bring this to a bug report level. What exactly is going wrong with the SCT? Does the workaround that stetim suggests work?


#7

I had, the same problem. Runningthe following code :


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

for turn in range(4):
  print "Turn", turn + 1
  guess_row = int(raw_input("Guess Row: "))
  guess_col = int(raw_input("Guess Col: "))

  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    break
  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"
    if (turn == 3):
      print "Game Over"
    print_board(board)

Gave me this message :
 
Traceback (most recent call last):
  File "python", line 27, in <module>
ValueError: invalid literal for int() with base 10: 'RUN'

stetim94's workaround worked : the code runs on its own as we don't have to input anything.
I could finish the lesson and  "play" a bit with the original code:

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

Then i tried to change a bit the code again and it blocked me again.

I hope those informations will be help full…


#8

Hi @oduffy,

What @stetim94 is referring to is an issue with the SCT where a raw_input() function will not allow the user to “input” anything; as it, for some reason, has automatically input itself. For example, if I did this:

def getResponse(response):
    isTrue = False
    response = response.lower()
    if response not in ["yes", "no"]:
        return "%s is not a valid response." % (response)
    else:
        if response == "yes":
            isTrue = True
        return response

print getResponse(raw_input("Yes or no? "))

The console would look like this without me doing anything other than hitting run:

> Yes or no? SCT
> SCT is not a valid response.

Note that I didn’t type “SCT”, I just hit run and the console inexplicably input SCT without me doing a single thing. This issue has been happening with this exercise and others for a few months now, and it never seems to go away. Hopefully my explanation was helpful.


#9

Thanks for the details, Colin! I’ve reported this one and will keep tabs on it.


#10

This error is still happenning.
Replied so thread wouldn’t close


#11

We understand that this error is still occurring, you don’t have to keep this thread open for us to continue working on it. We will let everyone know when this issue is fixed, until then keep in mind that even if a thread/topic is closed, moderators and admins can still reply to them.


#12

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