Bonsoir,

on nous a proposé un exercice en python: créer un programme affichant tous les carres magiques possibles avec que des chiffres différents... Je l'ai réussi, il fonctionne parfaitement mais je me retrouve avec des usines a gaz par endroit donc pourriez vous m'aider à l'améliorer svp

voici le code que j'ai actuellement:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#-*- coding: utf-8 -*-
 
from string import*
 
#----------------------Fonctions-du-programme----------------------#
 
def Saisie(L):
    # Saisie des chiffres du carre magique
    i=1
    while i<=3:
        l=list(input("Entrez les chiffres de la colonne %d separes par une virgule: "%i))
        if len(l)!=3:
            print 
            print "Le nombre de chiffre est incorrect! Veuillez ressaisir ceux-ci svp: "
            print 
        else:
            L.append(l)
            i+=1
    return L
 
def saisieAuto(L):
    for a in xrange(1,10):
        for b in xrange(1,10):
            if b!=a:
                for c in xrange(1,10):
                    if c!=a and c!=b:
                        for d in xrange(1,10):
                            if d!=c and d!=b and d!=a:
                                for e in xrange(1,10):
                                    if e!=d and e!=c and e!=b and e!=a:
                                        for f in xrange(1,10):
                                            if f!=e and f!=d and f!=c and f!=b and f!=a:
                                                for g in xrange(1,10):
                                                    if g!=f and g!=e and g!=d and g!=c and g!=b and g!=a:
                                                        for h in xrange(1,10):
                                                            if h!=g and h!=f and h!=e and h!=d and h!=c and h!=b and h!=a:
                                                                for i in xrange(1,10):
                                                                    if i!=h and i!=g and i!=f and i!=e and i!=d and i!=c and i!=b and i!=a:
                                                                        L=[[a,b,c],[d,e,f],[g,h,i]]
                                                                        if EstMagique(L)==1:
                                                                            Affichage(L)                                 
 
def Affichage(L):
    #Affichage de la grille du carre
    print
    print "Voici votre carre magique:"
    print
    for k in xrange(3):
        print "-------------"
        for j in xrange(3):
            print"!", L[j][k],
        print "!"
    print "-------------"
 
def sommeColonne(L):
    # Test de validite des colonnes
    for z in xrange(3):
        if sum(L[z])!=15:
            return 0
    return 1
 
def sommeLigne(L):
    # Test de validite des lignes
    for i in xrange(3):
        somme=0
        for y in xrange(3):
            somme=somme+L[y][i]
        if somme!=15:
            return 0
    return 1
 
def sommeDiagonale(L):
    # Test de validite des diagonales
    # Test pour la premiere diagonale
    somme1=0
    for k in xrange(3):
        somme1=somme1+L[k][k]
    if somme1!=15:
        return 0
    # Test pour la seconde diagonale
    somme2=0
    k=2
    g=0
    while g<3:
        somme2=somme2+L[g][k]
        k-=1
        g+=1
    if somme2!=15:
        return 0
    return 1
 
def EstMagique(L):
    # Test de validite du carre
    Test1=sommeColonne(L)
    Test2=sommeLigne(L)
    Test3=sommeDiagonale(L)
 
    if Test1*Test2*Test3==1:
        return 1
    return 0
 
def NombresDifferents(L):
    # On crée une nouvelle liste t pour pouvoir la scanner
    t=[L[i][k] for i in xrange(3) for k in xrange(3)]
 
    # On scanne la liste t afin de voir si chaque chiffre y est
    for chiffre in xrange(1,10):
            if chiffre not in t:            
                return 0
    return 1
#----------------------Prgramme Principal----------------------#
 
L=[]
v=raw_input("Pour entrer un carre tapez s:\nPour afficher tous les carres magiques possible tapez a: ")
if v=="s":
    Saisie(L)
    Affichage(L)
    print
    if EstMagique(L)==1:
        print "Votre carre est magique =)"
    else:
        print "Votre carre n'est pas magique :'("
    print
    if NombresDifferents(L)==1:
        print "Tous les chiffres sont différents."
    else:
        print "Des chiffres sont en double."
if v=="a":
    saisieAuto(L)