Python probleme avec "if"

Résolu
Semis127 Messages postés 8 Statut Membre -  
mamiemando Messages postés 33228 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,

J'ai un projet extrêmement important à rendre aujourd'hui. Il consiste à dessiner avec
turtle
une lettre choisie. Mais gros problème, quelle que soit la lettre choisie, le programme dessine la lettre A. Je ne sais pas comment faire aidez moi s'il vous plait.

Ci-dessous la partie du code :

Des fonctions ont été créé pour chaque lettre avec le code pour la dessiner.

if resultString.get() == "A" or "a" :
    a()
elif resultString.get() == "B" or "b" :
    b()
elif resultString.get() == "C" or "c" :
    c()
elif resultString.get() == "D" or "d" :
    d()
elif resultString.get() == "E" or "e" :
    e()
elif resultString.get() == "F" or "f" :
    f()
elif resultString.get() == "G" or "g" :
    g()
elif resultString.get() == "H" or "h" :
    h()
elif resultString.get() == "I" or "i" :
    i()
elif resultString.get() == "J" or "j" :
    j()
elif resultString.get() == "K" or "k" :
    k()
elif resultString.get() == "L" or "l" :
    l()
elif resultString.get() == "M" or "m" :
    m()
elif resultString.get() == "N" or "n" :
    n()
elif resultString.get() == "O" or "o" :
    o()
elif resultString.get() == "P" or "p" :
    p()
elif resultString.get() == "Q" or "q" :
    q()
elif resultString.get() == "R" or "r" :
    r()
elif resultString.get() == "S" or "s" :
    s()
elif resultString.get() == "T" or "t" :
    t()
elif resultString.get() == "U" or "u" :
    u()
elif resultString.get() == "V" or "v" :
    v()
elif resultString.get() == "W" or "w" :
    w()
elif resultString.get() == "X" or "x" :
    x()
elif resultString.get() == "Y" or "y" :
    y()
else :
    z()

4 réponses

  1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Bonjour,

    if resultString.get() == "A" or "a" :

    ça ne marche pas comme ça ... les "or" ..

    Il faut indiquer CHAQUE test ... le programme ne peut pas deviner que ton
    or "a"
    est en lien avec
    resultString.get()


    Donc :
    if resultString.get() == "A" or  resultString.get() == "a" :
    


    Le plus simple serait même de faire un upper sur ta variable pour ne tester que le A majuscule ...
    un truc du genre
    if resultString.get().upper() == "A"  :
    


    Et comme le dit Whism .. un Switch serait plus adapté dans ton cas.

    .
    1
    1. mamiemando Messages postés 33228 Date d'inscription   Statut Modérateur Dernière intervention   7 940
       
      Bonjour,

      Juste pour compléter les réponses de Whismeril et Jordane45, voici comment tu peux procéder (ici j'omets volontairement la partie turtle et tk car elles ne sont pas en rapport avec la question) :

      import sys
      
      def a():
          print("a")
      def b():
          print("b")
      # etc
      
      map_name_func = {
          f.__name__ : f
          for f in a, b # etc jusqu'à z
      }
      letter = input("Letter?")
      f = map_name_func.get(letter.lower())
      if f:
          f()
      else:
          print(f"Lettre {letter} invalide", sys.stderr)
      


      Bonne chance
      0
  2. Utilisateur anonyme
     
    Bonjour
    En Python, l’indentation est primordiale, or par défaut le site ne la conserve pas.
    Pour la conserver il faut utiliser les balises de code. Voir ce petit tuto https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
    On pourra commencer à essayer de t’aider quand tu auras reposté correctement ton code.
    0
    1. Semis127 Messages postés 8 Statut Membre
       
      Je n'arrive pas à le faire via ccm voici une capture du code.

      d'indenter, je vous envoie une photo du code directement.
      0
  3. Utilisateur anonyme
     
    Oui mais là, on ne peut pas copier coller pour essayer.
    Donc tu lis le petit tuto que je t'ai mis en lien et tu postes ton code correctement, s'il te plait.
    0
    1. Semis127 Messages postés 8 Statut Membre
       
      Merci, je vous mets le code en entier pour essayer.

      import turtle
      import tkinter
      from tkinter import ttk
      t = turtle
      
      mainapp = tkinter.Tk()
      mainapp.title("Affichage lettre")
      mainapp.geometry("200x100")
      mainapp.minsize(200, 100)
      mainapp.maxsize(800, 400)
      titre = tkinter.Label(mainapp, text = "Affichage lettre")
      
      def callbackFunc():
          resultString.set("{}".format(motString.get(),
                                            motString.get()))
      
      labelMot = tkinter.Label(mainapp,
                          text = "Entrer la lettre")
      labelMot.grid(column=0, row=1, sticky=tkinter.W)
      
      motString = tkinter.StringVar()
      entrymot = tkinter.Entry(mainapp, width=20, textvariable=motString)
      
      entrymot.grid(column=1, row=1, padx=10)
      
      
      resultButton = tkinter.Button(mainapp, text = 'Resultat',
                               command=callbackFunc)
      
      resultButton.grid(column=0, row=2, pady=10, sticky=tkinter.W)
      
      resultString=tkinter.StringVar()
      resultLabel = tkinter.Label(mainapp, textvariable=resultString)
      resultLabel.grid(column=1, row=2, padx=10, sticky=tkinter.W)
      resultString.get()
      titre.grid()
      
      
       
      mainapp.mainloop()
      
      print(resultString.get())
          
      tur = turtle.Turtle()
      tur.pensize(20)
      
      def a() :
          tur.forward(100)
          tur.left(120)
          tur.forward(100)
          tur.left(120)
          tur.forward(200)
          tur.penup()
          tur.goto(100, 0)
          tur.pendown()
          tur.right(120)
          tur.backward(100)
          
      def b() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.left(90)
          tur.forward(25)
          tur.circle(50, 180)
          tur.forward(25)
          tur.right(180)
          tur.forward(25)
          tur.circle(50, 180)
          tur.forward(25)
          
      def c() :
          tur.penup()
          tur.goto(0,125)
          tur.pendown()
          tur.left(180)
          tur.forward(25)
          tur.circle(100, 180)
          tur.forward(25)
          
      def d() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.left(90)
          tur.forward(25)
          tur.circle(100, 180)
          tur.forward(25)
          
      def e() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.left(90)
          tur.forward(100)
          tur.penup()
          tur.goto(0, 0)
          tur.pendown()
          tur.forward(75)
          tur.penup()
          tur.goto(0, 100)
          tur.pendown()
          tur.forward(100)
          
      def f() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.left(90)
          tur.penup()
          tur.goto(0, 0)
          tur.pendown()
          tur.forward(75)
          tur.penup()
          tur.goto(0, 100)
          tur.pendown()
          tur.forward(100)
          
      def g() :
          tur.penup()
          tur.goto(0,125)
          tur.pendown()
          tur.left(180)
          tur.forward(25)
          tur.circle(100, 180)
          tur.forward(25)
          tur.left(90)
          tur.forward(100)
          tur.left(90)
          tur.forward(50)
          
      def h() :
          tur.penup()
          tur.goto(-50, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.goto(-50, 0)
          tur.left(90)
          tur.forward(100)
          tur.goto(50, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          
      def i() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.penup()
          tur.goto(0, 150)
          tur.pendown()
          tur.circle(1, 360)
          
      def j() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.penup()
          tur.goto(0, 150)
          tur.pendown()
          tur.circle(1, 360)
          tur.penup()
          tur.goto(0, -100)
          tur.pendown()
          tur.circle(-75, 180)
          tur.fd(25)
          
      def k() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.penup()
          tur.goto(0,0)
          tur.pd()
          tur.left(45)
          tur.fd(140)
          tur.penup()
          tur.goto(0,0)
          tur.pd()
          tur.left(90)
          tur.fd(140)
      
      def l() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.left(90)
          tur.forward(100)
      
      def m() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.penup()
          tur.goto(0, 100)
          tur.pd()
          tur.left(45)
          tur.fd(140)
          tur.left(90)
          tur.fd(140)
          tur.right(135)
          tur.fd(200)
      
      def n() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.penup()
          tur.goto(0, 100)
          tur.pd()
          tur.left(30)
          tur.fd(230)
          tur.left(150)
          tur.fd(200)
          
      def o() :
          tur.penup()
          tur.goto(0, -100)
          tur.pd()
          tur.circle(100, 360)
          
      def p() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.penup()
          tur.goto(0, 100)
          tur.pd()
          tur.left(90)
          tur.fd(25)
          tur.circle(-50, 180)
          tur.fd(25)
      
      def q() :
          tur.penup()
          tur.goto(0, -100)
          tur.pd()
          tur.circle(100, 360)
          tur.penup()
          tur.goto(25, -25)
          tur.pd()
          tur.right(45)
          tur.fd(100)
          
      def r() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.penup()
          tur.goto(0, 100)
          tur.pd()
          tur.left(90)
          tur.fd(25)
          tur.circle(-50, 180)
          tur.fd(25)
          tur.left(135)
          tur.fd(140)
      
      def s() :
          tur.penup()
          tur.goto(0, 125)
          tur.pd()
          tur.left(180)
          tur.fd(25)
          tur.circle(50, 180)
          tur.circle(-50, 180)
          tur.fd(25)
          
      def t() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(200)
          tur.penup()
          tur.goto(75, 100)
          tur.pd()
          tur.left(-90)
          tur.fd(150)
          
      def u() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(90)
          tur.pendown()
          tur.forward(125)
          tur.circle(75, 180)
          tur.fd(125)
      
      def v() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(67.5)
          tur.pendown()
          tur.fd(213)
          tur.left(135)
          tur.fd(213)
          
      def w() :
          tur.penup()
          tur.goto(0, 100)
          tur.right(67.5)
          tur.pendown()
          tur.fd(213)
          tur.left(135)
          tur.fd(106.5)
          tur.right(135)
          tur.pendown()
          tur.fd(106.5)
          tur.left(135)
          tur.fd(213)
          
      def x() :
          tur.penup()
          tur.goto(0, 100)
          tur.pd()
          tur.right(45)
          tur.fd(200)
          tur.backward(100)
          tur.left(85)
          tur.fd(100)
          tur.backward(200)
          
      def y() :
          tur.penup()
          tur.goto(0, 100)
          tur.pd()
          tur.right(45)
          tur.fd(100)
          tur.left(85)
          tur.fd(100)
          tur.backward(200)
          
      def z() :
          tur.penup()
          tur.goto(0, 100)
          tur.pd()
          tur.fd(150)
          tur.right(140)
          tur.fd(190)
          tur.left(-220)
          tur.fd(150)
          
      
      if resultString.get() == "A" or "a" :
          a()
      elif resultString.get() == "B" or "b" :
          b()
      elif resultString.get() == "C" or "c" :
          c()
      elif resultString.get() == "D" or "d" :
          d()
      elif resultString.get() == "E" or "e" :
          e()
      elif resultString.get() == "F" or "f" :
          f()
      elif resultString.get() == "G" or "g" :
          g()
      elif resultString.get() == "H" or "h" :
          h()
      elif resultString.get() == "I" or "i" :
          i()
      elif resultString.get() == "J" or "j" :
          j()
      elif resultString.get() == "K" or "k" :
          k()
      elif resultString.get() == "L" or "l" :
          l()
      elif resultString.get() == "M" or "m" :
          m()
      elif resultString.get() == "N" or "n" :
          n()
      elif resultString.get() == "O" or "o" :
          o()
      elif resultString.get() == "P" or "p" :
          p()
      elif resultString.get() == "Q" or "q" :
          q()
      elif resultString.get() == "R" or "r" :
          r()
      elif resultString.get() == "S" or "s" :
          s()
      elif resultString.get() == "T" or "t" :
          t()
      elif resultString.get() == "U" or "u" :
          u()
      elif resultString.get() == "V" or "v" :
          v()
      elif resultString.get() == "W" or "w" :
          w()
      elif resultString.get() == "X" or "x" :
          x()
      elif resultString.get() == "Y" or "y" :
          y()
      else :
          z()
      
      0