13 Syntax Error


#1


It is giving me an invalid syntax message for this code


    elif board[guess_row][guess_col] == 'X':
        print "You guessed that one already."


What am I doing wrong?


#2

Hi can you post all your code?


#3

There should be nothing wrong syntactically with that code.
Are you sure the syntax error isn't coming from somewhere else in your code?


#4

This piece of code luks fine, please post your full code.


#5

Here you go

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!"
board[guess_row][guess_col] = "X"
print_board(board)
if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
else:
print "You missed my battleship!"
elif board[guess_row][guess_col] == "X":
print "You guessed that one already."

But it's saying the error is on line 39 and the elif is in the console in red text.


#6

Hello ,
Hello, I too am stuck at this level ... I think I have the same kind of error but I do not see how to fix. (I'm on for almost 12 hours and I consult the forum without success!)

Here is my code:

from random import randint

plateau = []

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

def afficher_plateau(plateau):
    for ligne in plateau:
        print " ".join(ligne)

afficher_plateau(plateau)

def alea_ligne(plateau):
    return randint(0, len(plateau) - 1)

def alea_col(plateau):
    return randint(0, len(plateau[0]) - 1)

bateau_x = alea_ligne(plateau)
bateau_y = alea_col(plateau)
tir_x = int(raw_input("Quelle ligne ?"))
tir_y = int(raw_input("Quelle colonne ?"))

print bateau_x 
print bateau_y 

# Ajoutez votre code ci-dessous :
if tir_x == bateau_x and tir_y == bateau_y :
    print "Bravo ! Vous avez eu mon bateau !"

elif plateau[alea_ligne][alea_col] == "X":
    print "Cette case est deja decouverte."
    
else:
    if alea_ligne not in range(5) or alea_col not in range(5):
        print "Oups ! Vous devez viser l'ocean"
    
    else:
        print "Dommage !"

plateau[bateau_x][bateau_y]== "X"   
print plateau

And this is what comes out the console:

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
Quelle ligne ? 5
Quelle colonne ? 3
1
4
Traceback (most recent call last):
  File "python", line 32, in <module>
TypeError: list indices must be integers, not function

Thank you to those who can help me :slightly_smiling:


#7

Hi,

Good English! I'm so jealous of bilingual people!

Your area_ligne is a function which needs a value passed to it I think. The console is expecting something like area_ligne(plateau) or area_ligne(variable) on line 32, not just the name of the function on its own.

I hope this helps.


#8

You can't normally have an elif after an else. Correct syntax is:

if A:
    do B
elif C:
    do C
else:
    do D

Can you swap the elif and else parts round and achieve the desired result?

If this is not the problem then I would suggest resending your code including the whitespaces just to make it easier to read and spot any white space errors.

Hope this helps


#9

Hi,

My English is very bad :slightly_smiling:
I used my google translation ^^

(But I'm still bilingual: p lol French & Creole)

thank you for the tip I'll test ...