I am confused about the code I wrote


#1



I wrote the code and I passed the section but I believe it is not a true code because instead of turning the player to the second guess it shows this :
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
Guess Row: 1
Guess Col: 2
2
2
You missed my battleship!
Noch
O O O O O
O O X O O
O O O O O
O O O O O
O O O O O
None
1
2
3
4
None


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

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
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 "Noch"
    board[guess_row][guess_col] = "X"
    print print_board(board)
    
for turn in range(4):
    print turn + 1


#2

look at the comment:

# 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:"))

it gives you a good hint about where you have to place the loop, you placed it wrong


#3

Thank you so much I am trying to fix it. It is not fixed yet! I need to work on it


#4

If you need more help, post an updated version of your code and i will help you further

but solving it yourself is of course better and more satisfying


#5

Thank you so much yes. You are true. Actually this time by changing the place of that hint you gave me it worked but it does not pass the section and the problem is somewhere else now! It is strange but If I really could understand the whole classes I could find what is the problem! Maybe my problem is still I have problem in understanding


#6

for turn in range(4):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
print ship_row
print ship_col
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
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 "Noch"

board[guess_row][guess_col] = "X"
print print_board(board)

print turn + 1


#7

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
Guess Row: 9
Guess Col: 9
3
0
You missed my battleship!
Oops, that's not even in the ocean.
Guess Row: 5
Guess Col: 4
3
0
You missed my battleship!
Oops, that's not even in the ocean.
Guess Row: 2
Guess Col: 4
3
0
You missed my battleship!
Noch
Guess Row: 6
Guess Col: 5
3
0
You missed my battleship!
Oops, that's not even in the ocean.
Traceback (most recent call last):
File "python", line 41, in
IndexError: list index out of range


#8

I am not sure If i must copy my new code here or in a new post. If I need update my code everytime in a new post, plz let me know to obey the rule of the website and format. And thank you for the time you put for me


#9

I found it. I found it

hip_row = random_row(board)
ship_col = random_col(board)

for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    print ship_row
    print ship_col
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
    else:
        print "You missed my battleship!"
    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 "Noch"
        board[guess_row][guess_col] = "X"
        print print_board(board)
    print turn + 1

#10

I need to notice more about details coz programming is all about detail. Dear Stetim94 Thanks a million for the hint and for great help


#11

personally i would do this:

ship_row = random_row(board)
ship_col = random_col(board)

for turn in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    print ship_row
    print ship_col
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sank my battleship!"
    else:
        print "You missed my battleship!"
        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 "Noch"
            board[guess_row][guess_col] = "X"
            print print_board(board)
    print turn + 1

this way, the last if/elif and else only runs when needs be

you could agrue about the indent of this line:

print print_board(board)

i will leave that to you


#12

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