16. Game Over


#1



Python / List and Functions / Battleships / 16. Game over


Turn is not defined and I don't really understand what I have to do


Replace this line with your code. 
from random import randint

board = []

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

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

print "Let's play Battleship!"
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

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
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 sunk my battleship!"
else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        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 (turn + 1) here!
for turn in range(4):
    print "Turn", turn + 1
    print ship_row
    print ship_col
    
    print_board(board)


#2

see this comments:

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!

turn is the loop iterator, but your loop is at the wrong place.


#3

Should it be above the if statement


#4

Now it just says that I didn't add the if statement with Game Over


#5

i can't see what you changed, can you post an updated version of your code?


#6

from random import randint

board = []

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

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

print "Let's play Battleship!"
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

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

for turn in range(4):
    print "Turn", turn + 1
    print ship_row
    print ship_col

if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        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 (turn + 1) here!

    
    print_board(board)

#7

why did you move your print turn? I said your for loop is at the wrong place, said nothing about your print turn.

You really need to add more to your for loop if you want the user to have multiply guesses and that after each guess check if the battle ship is hit, this is currently not happening

Walk through your game, does it make sense at the moment? Does the user gets multiply guesses? does each guess get checked?


#8

I'm sorry I just really don't understand what to do


#9

okay, so i took another look at the lesson. The reason you are absolutely confused is because the code you have doesn't match with what you start with in this exercise.

This works really confusing. 15. play it sam, can you copy the code you have in this exercise to the forum?


#10

Thank you so much I will try this again now


#11

It works now, thanks


#12

if you need more help, post an updated version of your code


#13

A post was split to a new topic: 16. game over


#14

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