Zillay
Zillay

Reputation: 1

unorderable types: int() > range()

I have a task of creating battleships in Python, when the user inputs co-ordinates out of the 5x5 grid, I want a pop up stating it is out of range. My problem is I get an error message stating "unorderable types: int() > range()", I'm new to Python so I do not know what that means.

if guess_row == ship_row and guess_col == ship_col:
    print ("Congratulations, you sank my battleship!")
else:
    if guess_row > range(5) or guess_col > range(5):
        print ("Out of Range!")
    else:
        print ("You missed my battleship")
        grid[guess_row][guess_col]="X"
        print_grid(grid)

Upvotes: 0

Views: 197

Answers (1)

Alex Hall
Alex Hall

Reputation: 36033

range(5) is a range (actually it's the list [0, 1, 2, 3, 4]), not a number. How can a number be greater than a range? It can't, hence the error.

These will work and are all equivalent:

  • guess_row not in range(5)
  • guess_row not in range(0, 5)
  • not (0 <= guess_row < 5)
  • not (0 <= guess_row <= 4)
  • guess_row < 0 or guess_row >= 5
  • guess_row < 0 or guess_row > 4

Upvotes: 1

Related Questions