Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
984 connectés 

  FORUM HardWare.fr
  Programmation
  Python

  Damier sur python

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Damier sur python

n°2242332
desperadou​u
Posté le 05-11-2014 à 20:59:52  profilanswer
 

Bonjour je veux réaliser un damier sur python avec deux boucles for mais je n'y arrive pas , quelqu'un pourrait-il m'aider ? Merci
 
cpp]from tkinter import *
fen1=Tk()
x1,y1,x2,y2,coul=0,0,20,20,"black"
 
def damier():
    global x1,y1,x2,y2,coul
    for i in range (10):
        x1=0
        for j in range (9):
            can.create_rectangle(x1,y1,x2,y2,fill=coul)
            x1+=20
    y1+=20
 
 
 
can=Canvas(fen1,height=180,width=200,bg="white" )
can.pack()
bou1=Button(fen1,text="Dessiner damier",command=damier)
bou1.pack()
bou2=Button(fen1,text="Quitter",command=fen1.quit)
bou2.pack()
fen1.mainloop()
fen1.destroy()[/cpp]

mood
Publicité
Posté le 05-11-2014 à 20:59:52  profilanswer
 

n°2242334
rage2000
Posté le 05-11-2014 à 22:18:44  profilanswer
 

Le probleme vient de ta fonction damier:
 
1/ tes X2 et y2 restent toujours fixe alors que X1 et Y1 bougent. De manière général X2 et Y2 servent a rien car ca sera toujours X1+20 et Y1+20 (du coup on peut simplifier avec x et y)
 
2/ quand tu changes de ligne, tu commences toujours avec un carré noir, alors qu'il faut alterner
 
3/ C'est pas une erreur (c'est possible de le faire comme ca) mais il y a quand même un soucis algorithmique a utiliser des for sur une variable qui te sert a rien (i et j). 2 solutions soit tu les remplaces par un while sur x et y, soit tu fais un for mais en profite pour incrémenté tes x et y avec.
 
ps: au passage j'ai viré tes variables globales, elles servent a rien et les variables globales c'est quand même le mal en programmation. ;)
 
Solution avec des while:

Code :
  1. def damier():
  2.     y = 0
  3.     coul = "black"
  4.     start_black = True
  5.     while y < 180:
  6.         if start_black:
  7.             x = 0
  8.         else:
  9.             x = 20
  10.         while x < 200:
  11.             can.create_rectangle(x,y,x+20,y+20,fill=coul)
  12.             x += 40
  13.         y += 20
  14.         start_black = not start_black


 
solution avec les for (je préfère celle la, plus pythonique)

Code :
  1. def damier():
  2.     coul = "black"
  3.     start_black = True
  4.     for y in range(0, 180, 20):
  5.         if start_black:
  6.             init_x = 0
  7.         else:
  8.             init_x = 20
  9.         for x in range(init_x, 200, 40):
  10.             can.create_rectangle(x,y,x+20,y+20,fill=coul)
  11.         start_black = not start_black


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Python

  Damier sur python

 

Sujets relatifs
programmation en python, petit blocagePython programme aide
Programme faux en python? avant l'instalation de python sur pc avec serveur apache
lancer des commandes python en paralleleJeu de roulette en python
Comment faire tourner une spirale avec python ?[Python] Tri tableau
[Python 3] Lister fichiers présents sur un serveur HTTP[Python 3.X] Utilisation de urllib
Plus de sujets relatifs à : Damier sur python


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR