1

I'm trying to code a battleships game in python and all my code seems to be working except for one specific line in which the program returns with an error under the form of:

"TypeError: unorderable types str() < int()"

When I try run the code on my portable PyScripter the program seems to highlight this line specifically

if (Guess_Board_Row < 0 or Guess_Board_Row > 4) or (Guess_Board_Column < 0 
or Guess_Board_Column > 4):

This is my code in it's entirity:

import random


Battleship_Board =[]


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


def print_Battleship_Board(Battleship_Board):
    for row in Battleship_Board:
       print (" ".join(row))


def Random_Board_Row(Battleship_Board):
return random.randint(0, len(Battleship_Board)-1)

def Random_Board_Column(Battleship_Board):
    return random.randint(0, len(Battleship_Board[0])-1)

Battleship_Row = Random_Board_Row(Battleship_Board)
Battleship_Column = Random_Board_Column(Battleship_Board)
print("Row:", Battleship_Row)
print("Column:", Battleship_Column)

for turn in range(4):
Guess_Board_Row = input("Enter a row value")
Guess_Board_Column = input("Enter a column value")

if Guess_Board_Row == Battleship_Row and Guess_Board_Column == Battleship_Column:
    print("You sunk my battleship!")
    break
else:
    if turn == 4:
        Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"
        print_Battleship_Board(Battleship_Board)
        print("Game Over")
        print("My Ship was here: [" + str(Battleship_Row) + "][" + str(Battleship_Column) + "]")
    else:
        if (Guess_Board_Row < 0 or Guess_Board_Row > 4) or (Guess_Board_Column < 0 or Guess_Board_Column > 4):
            print("Apologies, that's not on the grid")
        elif(Battleship_Board[Guess_Board_Row][Guess_Board_Column] == "X"):
            print("You already guessed that value")
        else:
            print("You missed my battleship!")
            Battleship_Board[Guess_Board_Row][Guess_Board_Column] = "X"
        print(turn + 1)
        print_Battleship_Board(Battleship_Board)

What is the issue and how can I go about fixing it?

Henry Green
  • 243
  • 3
  • 4
  • 15
  • The error message tells you *precisely* what the problem is, and there are dozens of questions referring to how to fix it on SO already (just look at the **Related** sidebar, for example...) - you have a string, but you want an integer. – jonrsharpe Mar 28 '16 at 21:02
  • Posting the entire error and traceback is helpful. But basically, you're comparing apples to oranges or in this case strings to integers – jDo Mar 28 '16 at 21:03

0 Answers0