π
<-

Petite erreur pour génération de labyrinthe en python

TI's micropython + modules

Petite erreur pour génération de labyrinthe en python

Unread postby Altaear » 22 Apr 2022, 19:38

Hello j'ai un petit problème avec python sur calculatrice,
J'ai récupéré ce bout de code pour générer un labyrinthe mais il m'affiche quelques erreurs, est ce que quelqu'un sait ou j'ai pu me gourrer ?
Code: Select all
from ti_draw import *
from random import *

def vline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1,y1+wl)

def hline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1+wl,y1)

def cell(x,y):
  z=a[x+y*w]
  hline(x,y,z&1)
  vline(x+1,y,z&2 )
  hline(x,y+1,z&4)
  vline(x,y,z&8)

def disp():
  clear
  for x in range(w):
    for y in range(h):
      cell(x,y)

def nor(u):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
    return u

def eas(u):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&13
      a[v]&7
      return v
    return u

def sou(u):
  v=u+w
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
    return u

def wes(u):
  v=u-1
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
    return u

def move(u):
  for i in range(c):
    d=randrange(4)
    if d==0:
      u=nor(u)
    if d==1:
      u=eas(u)
    if d==2:
      u=sou(u)
    if d==3:
      u=wes(u)

# Initialize
w=25
h=17
n=w*h
c=n/4
wl=12
a=[15 for x in range(n)]
t=[x for x in range(n)]
a[0]=7
clear()
draw_text(125,110,"-thinking-")

while 1:
 
  # Shuffle the index
  for i in range(n):
    j=randrange(n)
    t[i],t[j]=t[j],t[i]
 
  #Check each cell in index order
  done=True
  for i in range(n):
    u=t[i]
    if a[u]==15:
      done=False
    else:
      move(u)

#Done if all cells connected
  if done:
    break

#Done
a[n-1]&=13
disp()


Voici mon erreur :
Code: Select all
File "<stdin>", line 2, in <module>
  File "C:\Users\arihe\AppData\Roaming\Texas I
nstruments\TI-Nspire CX CAS Student Softwar
e\python\doc2\maze.py", line 103, in <module>
  File "C:\Users\arihe\AppData\Roaming\Texas I
nstruments\TI-Nspire CX CAS Student Softwar
e\python\doc2\maze.py", line 71, in move
  File "C:\Users\arihe\AppData\Roaming\Texas I
nstruments\TI-Nspire CX CAS Student Softwar
e\python\doc2\maze.py", line 39, in eas
TypeError: unsupported types for __add__: 'Non
eType', 'int'
User avatar
Altaear
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 55.5%
 
Posts: 17
Joined: 08 Sep 2019, 19:04
Gender: Not specified
Calculator(s):
MyCalcs profile
Class: Term

Re: Petit erreur pour génération de labyrinthe en python

Unread postby critor » 22 Apr 2022, 20:18

Bonjour.

De ce que je constate, tes fonctions opératoires (nor, eas, sou, wes) ont toutes leurs instructions return dans des blocs conditionnels, et ce sans aucune alternative (else).
Peut-être que le cas ne serait pas censé se produire, mais a priori je ne suis absolument pas surpris qu'elles puissent renvoyer None, ce qui fait alors échouer l'opération suivante comme indiqué par tes messages d'erreurs.
Image
User avatar
critorAdmin
Niveau 19: CU (Créateur Universel)
Niveau 19: CU (Créateur Universel)
Level up: 53.2%
 
Posts: 42388
Images: 17088
Joined: 25 Oct 2008, 00:00
Location: Montpellier
Gender: Male
Calculator(s):
MyCalcs profile
YouTube: critor3000
Twitter: critor2000
GitHub: critor

Re: Petit erreur pour génération de labyrinthe en python

Unread postby critor » 22 Apr 2022, 20:34

J'ai retrouvé le script original :
https://fr1lib.org/book/17194983/a0aa7c

Et je te confirme bien dans ta transcription une erreur d'indentation des return dans les fonctions opératoires.

Cela devrait être ça :
Code: Select all
def nor(u):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
  return u

def eas(u):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&=13
      a[v]&=7
      return v
  return u

def sou(u):
  v=u+w
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
  return u

def wes(u):
  v=u-1
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
  return u
Image
User avatar
critorAdmin
Niveau 19: CU (Créateur Universel)
Niveau 19: CU (Créateur Universel)
Level up: 53.2%
 
Posts: 42388
Images: 17088
Joined: 25 Oct 2008, 00:00
Location: Montpellier
Gender: Male
Calculator(s):
MyCalcs profile
YouTube: critor3000
Twitter: critor2000
GitHub: critor

Re: Petit erreur pour génération de labyrinthe en python

Unread postby Altaear » 22 Apr 2022, 20:43

En effet je n'ai plus d'erreur :)
Mais malheureusement la génération du labyrinthe bug un peu :/
Ca peut paraitre bête mais j'ai recopié ce code d'un livre pour essayer de le comprendre mais je ne connais pas les &= en python à quoi servent-ils ?
User avatar
Altaear
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 55.5%
 
Posts: 17
Joined: 08 Sep 2019, 19:04
Gender: Not specified
Calculator(s):
MyCalcs profile
Class: Term

Re: Petit erreur pour génération de labyrinthe en python

Unread postby critor » 22 Apr 2022, 20:57

Je confirme que c'est long, mais que ça finit bien par marcher :
Image

&= est un raccourci pour réaliser un "et bit à bit" sur la variable concernée.
Par exemple, les 2 lignes suivantes sont équivalentes :
Code: Select all
a[u] &= 14
a[u] = a[u] & 14
Image
User avatar
critorAdmin
Niveau 19: CU (Créateur Universel)
Niveau 19: CU (Créateur Universel)
Level up: 53.2%
 
Posts: 42388
Images: 17088
Joined: 25 Oct 2008, 00:00
Location: Montpellier
Gender: Male
Calculator(s):
MyCalcs profile
YouTube: critor3000
Twitter: critor2000
GitHub: critor

Re: Petit erreur pour génération de labyrinthe en python

Unread postby Altaear » 22 Apr 2022, 21:28

D'accord je comprend à peu près,
Mais malheureusement moi mon programme à décider que ça ne marcherait pas :sob:
You do not have the required permissions to view the files attached to this post.
User avatar
Altaear
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 55.5%
 
Posts: 17
Joined: 08 Sep 2019, 19:04
Gender: Not specified
Calculator(s):
MyCalcs profile
Class: Term

Re: Petit erreur pour génération de labyrinthe en python

Unread postby critor » 22 Apr 2022, 21:37

Ouille...

Je te colle exactement ce que j'ai fait tourner ici :
Code: Select all
from ti_draw import *
from random import *

def vline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1,y1+wl)

def hline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1+wl,y1)

def cell(x,y):
  z=a[x+y*w]
  hline(x,y,z&1)
  vline(x+1,y,z&2 )
  hline(x,y+1,z&4)
  vline(x,y,z&8)

def disp():
  clear
  for x in range(w):
    for y in range(h):
      cell(x,y)

def nor(u):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
  return u

def eas(u):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&=13
      a[v]&=7
      return v
  return u

def sou(u):
  v=u+w
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
  return u

def wes(u):
  v=u-1
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
  return u

def move(u):
  for i in range(c):
    d=randrange(4)
    if d==0:
      u=nor(u)
    if d==1:
      u=eas(u)
    if d==2:
      u=sou(u)
    if d==3:
      u=wes(u)

# Initialize
w=25
h=17
n=w*h
c=n/4
wl=12
a=[15 for x in range(n)]
t=[x for x in range(n)]
a[0]=7
clear()
draw_text(125,110,"-thinking-")

while 1:

  # Shuffle the index
  for i in range(n):
    j=randrange(n)
    t[i],t[j]=t[j],t[i]

  #Check each cell in index order
  done=True
  for i in range(n):
    u=t[i]
    if a[u]==15:
      done=False
    else:
      move(u)

#Done if all cells connected
  if done:
    break

#Done
a[n-1]&=13
disp()
Image
User avatar
critorAdmin
Niveau 19: CU (Créateur Universel)
Niveau 19: CU (Créateur Universel)
Level up: 53.2%
 
Posts: 42388
Images: 17088
Joined: 25 Oct 2008, 00:00
Location: Montpellier
Gender: Male
Calculator(s):
MyCalcs profile
YouTube: critor3000
Twitter: critor2000
GitHub: critor

Re: Petit erreur pour génération de labyrinthe en python

Unread postby Altaear » 22 Apr 2022, 21:50

en effet il manquait un = après le & dans ma méthode eas merci pour la correction :)
Une dernière chose le thinking reste en plein milieu de l'écran comment puis-je le supprimer ?
User avatar
Altaear
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 55.5%
 
Posts: 17
Joined: 08 Sep 2019, 19:04
Gender: Not specified
Calculator(s):
MyCalcs profile
Class: Term

Re: Petit erreur pour génération de labyrinthe en python

Unread postby critor » 22 Apr 2022, 22:05

J'ai pareil ici.

La fonction disp() est pourtant prévue pour commencer par tout effacer avec un appel clear().

Mais j'ai l'impression qu'il y a eu une autre erreur de copie dans ton script plus haut (ou dans la source que tu as utilisée), cet appel est mal écrit : clear au lieu de clear().

Voici le code corrigé de nouveau :
Code: Select all
from ti_draw import *
from random import *

def vline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1,y1+wl)

def hline(x,y,w):
  if w:
    x1=x*wl+9
    y1=y*wl+3
    draw_line(x1,y1,x1+wl,y1)

def cell(x,y):
  z=a[x+y*w]
  hline(x,y,z&1)
  vline(x+1,y,z&2 )
  hline(x,y+1,z&4)
  vline(x,y,z&8)

def disp():
  clear
  for x in range(w):
    for y in range(h):
      cell(x,y)

def nor(u):
  v=u-w
  if v>0:
    if a[v]==15:
      a[u]&=14
      a[v]&=11
      return v
  return u

def eas(u):
  v=u+1
  if v%w:
    if a[v]==15:
      a[u]&=13
      a[v]&=7
      return v
  return u

def sou(u):
  v=u+w
  if v<n:
    if a[v]==15:
      a[u]&=11
      a[v]&=14
      return v
  return u

def wes(u):
  v=u-1
  if u%w:
    if a[v]==15:
      a[u]&=7
      a[v]&=13
      return v
  return u

def move(u):
  for i in range(c):
    d=randrange(4)
    if d==0:
      u=nor(u)
    if d==1:
      u=eas(u)
    if d==2:
      u=sou(u)
    if d==3:
      u=wes(u)

# Initialize
w=25
h=17
n=w*h
c=n/4
wl=12
a=[15 for x in range(n)]
t=[x for x in range(n)]
a[0]=7
clear()
draw_text(125,110,"-thinking-")

while 1:

  # Shuffle the index
  for i in range(n):
    j=randrange(n)
    t[i],t[j]=t[j],t[i]

  #Check each cell in index order
  done=True
  for i in range(n):
    u=t[i]
    if a[u]==15:
      done=False
    else:
      move(u)

#Done if all cells connected
  if done:
    break

#Done
a[n-1]&=13
disp()


Et ça marche : :)
Image
Image
User avatar
critorAdmin
Niveau 19: CU (Créateur Universel)
Niveau 19: CU (Créateur Universel)
Level up: 53.2%
 
Posts: 42388
Images: 17088
Joined: 25 Oct 2008, 00:00
Location: Montpellier
Gender: Male
Calculator(s):
MyCalcs profile
YouTube: critor3000
Twitter: critor2000
GitHub: critor

Re: Petit erreur pour génération de labyrinthe en python

Unread postby Altaear » 22 Apr 2022, 22:08

Merci beaucoup pour le temps pris :D
User avatar
Altaear
Niveau 8: ER (Espèce Rare: nerd)
Niveau 8: ER (Espèce Rare: nerd)
Level up: 55.5%
 
Posts: 17
Joined: 08 Sep 2019, 19:04
Gender: Not specified
Calculator(s):
MyCalcs profile
Class: Term

Next

Return to Python

Who is online

Users browsing this forum: ClaudeBot [spider] and 18 guests

-
Search
-
Social TI-Planet
-
Featured topics
Comparaisons des meilleurs prix pour acheter sa calculatrice !
"1 calculatrice pour tous", le programme solidaire de Texas Instruments. Reçois gratuitement et sans aucune obligation d'achat, 5 calculatrices couleur programmables en Python à donner aux élèves les plus nécessiteux de ton lycée. Tu peux recevoir au choix 5 TI-82 Advanced Edition Python ou bien 5 TI-83 Premium CE Edition Python.
Enseignant(e), reçois gratuitement 1 exemplaire de test de la TI-82 Advanced Edition Python. À demander d'ici le 31 décembre 2024.
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
1234
-
Donations / Premium
For more contests, prizes, reviews, helping us pay the server and domains...
Donate
Discover the the advantages of a donor account !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partner and ad
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
1804 utilisateurs:
>1750 invités
>47 membres
>7 robots
Record simultané (sur 6 mois):
6892 utilisateurs (le 07/06/2017)
-
Other interesting websites
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)