Battleship 17/19


#1

https://www.codecademy.com/fr/courses/python-beginner-fr-FR-07uaf/2/3?curriculum_id=5370f030fed2a866a2000001#


My code doesn't work. "break outside the loop" ?

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)
print bateau_x 
print bateau_y 

# A partir d'ici tout le reste doit etre dans votre boucle for !
# N'oubliez pas l'indentation !
for tour in range(4):
    print "Tour", tour+1
tir_x = int(raw_input("Quelle ligne ?"))
tir_y = int(raw_input("Quelle colonne ?"))

if tir_x == bateau_x and tir_y == bateau_y:
    print "Bravo ! Vous avez eu mon bateau !"
    break
else:
    if (tir_x < 0 or tir_x > 4) or (tir_y < 0 or tir_y > 4):
        print "Oups ! Vous devez viser l'ocean."
    elif(plateau[tir_x ][tir_y ] == "X"):
        print "Cette case est deja decouverte."
    if tour == 3:
        print "Game Over"
    else:
        print "Dommage !"
        plateau[tir_x ][tir_y ] = "X"
    print tour+1# Affichez tour+1 ici !
afficher_plateau(plateau)

  **File "python", line 34**
**SyntaxError: 'break' outside loop**


#2

The break keyword is used to stop a loop or a switch-statement. It can't be used in and if-statement.


#3

So where should i put my break ?


#4

Ok i have resolved it.
Thanks again.