Battleship 15/19


#1



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


Why my code doesn't work please ?

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"
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 !"
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."
    else:
        print "Dommage !"
        plateau[tir_x ][tir_y ] = "X"
    print tour+1# Affichez tour+1 ici !
affiche_plateau(plateau)

In 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
4
3
Tour
Tour
Tour
Tour
Quelle ligne ? 2
Quelle colonne ? 3
Dommage !
4
Traceback (most recent call last):
  File "python", line 43, in <module>
NameError: name 'affiche_plateau' is not defined


#2

In your code you define the function afficher_plateau but on the end of the program, you call the function affiche_plateau. A small typo.


#3

OK. A small typo.:sunglasses:
Thanks a lot.


#4

You're welcome. Small mistakes are always hard to find :smile: