How to make the valid guess to be between 1 - 5 and not 0 - 4


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/2/5?curriculum_id=4f89dab3d788890003000096#


I would like to make the valid inputs to be between 1 - 5 instead of 0 - 4. Which means that row 0 and col 0 should be hit with a guess_row of 1 and guess_col of 1. If you got a tip or trick to lead me in the right direction, please share it! Thanks


board = []

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

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

# Hide a ship on the 5x5 board:
from random import randint

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

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

# Let the player guess where the ship is hidden:
for turn in range(6):
    print "Enter a guess between 0 and 4."
    guess_row = int(raw_input("Guess Row: "))
    guess_col = int(raw_input("Guess Column: ")) 

    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"
    
    print_board(board)
    turn += 1 
    print "Turn",turn,"out of 6 has ended!"
    print ""
    if turn == 6:
        print "Game over!"


#2

You could have the user enter 1 through 5 and just subtract one from their guess or you could alter the ranges in your code. range(1,6)


#3

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