IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Premier programme python petit, simple mais buggé


Sujet :

Python

  1. #1
    Nouveau Candidat au Club
    Femme Profil pro
    Ingénieur agronome
    Inscrit en
    Janvier 2016
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Autriche

    Informations professionnelles :
    Activité : Ingénieur agronome
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2016
    Messages : 3
    Points : 1
    Points
    1
    Par défaut Premier programme python petit, simple mais buggé
    Bonjour,
    Il s´agit de mon premier programme et bien évidement il ne marche pas
    J´ai bien lu les consignes sur le titre clair mais comme je ne vois pas d´ou vient mon problème je ne sais pas quoi mettre dans le titre.
    Alors voilà la bête:
    Le premier fichier.py s´appelle FDKrunning.py et contient:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import os
    from PIL.Image import *
    from ProgFDK3 import *
     
    #ask picture´s name
    Name=input("enter the picture´s name")
     
     
    # test the function FDK_evaluation from the module ProgFDK3
    FDK_evaluation(Name)
     
    os.system("pause")[/I]
    Le second fichier ProgFDK3.py contient:
    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
     
    """Module ProgFDK3 including the funtion FDK_evaluation"""
     
     
    def FDK_evaluation(Name,paramB=100,paramR=79):
     
     """This function evaluate the FDK symptomes on grains
            The first variable contains the name of the picture
            The second the blue parameter which allow to recognize the diseased-grains
             and which is automatically set up to 100 for triticale
            The third variable is the red parameter which allow to recognize our blue background
             and which is automatically set up to 79"""
     
    #Load the picture
    i_orig = open(Name)
    i_trans = open(Name)
     
    #Show it
    #Image.show(i_orig)
     
    # Get the size in pixels of the picture
    (largeur, hauteur)= i_orig.size
    # initialize the number of pixel from grains and from diseased grains
    tot_pix_grains = 0
    nb_pix_diseased = 0
     
    for x in range(largeur):
        for y in range(hauteur):
        # Here we check the pixel with the coordinates(x,y) from the picture i-orig.
        # The point(0,0) is in the top left-hand corner.
            #getpixel, will get the level of red, green, and blue from the pixel
            (r,g,b) = i_orig.getpixel((x,y))
            # if r>79 we are not looking a pixel in the blue background.
            if r>79:
                #if this pixel is not in the background it is in a grain.
                tot_pix_grains += 1
                # if this pixel present a level of b>100 it is turning white. it is diseased.
                if b>100 :
                      nb_pix_diseased += 1
                #Now we change its color
                (l,m,n) = (204,153,255)
                i_trans.putpixel((x,y),(l,m,n))
     
            else :
            # if r<80 we are looking a pixel in the blue background.    
                #Now we turn it in black
                (l,m,n) = (0,0,0)
                i_trans.putpixel((x,y),(l,m,n))
     
     
     
     
    #Image.show(i_trans)            
    print("number of grain-pixel =", tot_pix_grains, "number of diseased-grain-pixel =", nb_pix_diseased, "% =", nb_pix_diseased*100/tot_pix_grains)
    Et quand je souhaite le lancer j´obtiens la réponse NameError: name 'Name' is not defined pourtant Name est le paramètre de ma fonction et devrait m´être demandé grâce à input donc je ne comprend pas ce qui ne va pas.
    Je précise que les deux fichiers.py sont rangés dans le même dossier et qu´un dossier __pycache__ a bien été créé.

    Merci pour vos explications !

  2. #2
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 298
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 298
    Points : 6 778
    Points
    6 778
    Par défaut
    Salut,

    Dans la fonction FDK_evaluation() il faut que tu décales tout le code d'une tabulation.

  3. #3
    Nouveau Candidat au Club
    Femme Profil pro
    Ingénieur agronome
    Inscrit en
    Janvier 2016
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Autriche

    Informations professionnelles :
    Activité : Ingénieur agronome
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2016
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Bonjour VinsS,
    Je viens de le faire (voir code ci-joint) mais ça n´a rien changé. Et lorsque je recherche des bug avec IDLE je reçoit le message "unexpected indent" au niveau de la ligne 15. C´est d´ailleurs à cause de ce message que j´avais retiré une tab à tous les niveaux.

    Sinon ma rédaction sur la ligne 44 est elle correcte? Le else correspond au premier if et non pas au second. j´ai donc mis un espace pour le séparer du 2e if et une tab en moins. Le fait qu´il n´y ait pas de end me perturbe.

    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
    """Module ProgFDK3 including the funtion FDK_evaluation"""
     
     
    def FDK_evaluation(Name,paramB=100,paramR=79):
     
     """This function evaluate the FDK symptomes on grains
            The first variable contains the name of the picture
            The second the blue parameter which allow to recognize the diseased-grains
             and which is automatically set up to 100 for triticale
            The third variable is the red parameter which allow to recognize our blue background
             and which is automatically set up to 79"""
     
        #Load the picture
        i_orig = open(Name)
        i_trans = open(Name)
     
        #Show it
        #Image.show(i_orig)
     
        # Get the size in pixels of the picture
        (largeur, hauteur)= i_orig.size
        # initialize the number of pixel from grains and from diseased grains
        tot_pix_grains = 0
        nb_pix_diseased = 0
     
        for x in range(largeur):
            for y in range(hauteur):
            # Here we check the pixel with the coordinates(x,y) from the picture i-orig.
            # The point(0,0) is in the top left-hand corner.
                #getpixel, will get the level of red, green, and blue from the pixel
                (r,g,b) = i_orig.getpixel((x,y))
                # if r>79 we are not looking a pixel in the blue background.
                if r>79:
                    #if this pixel is not in the background it is in a grain.
                    tot_pix_grains += 1
                    # if this pixel present a level of b>100 it is turning white. it is diseased.
                    if b>100 :
                        nb_pix_diseased += 1
                        #Now we change its color
                        (l,m,n) = (204,153,255)
                        i_trans.putpixel((x,y),(l,m,n))
     
                else :
                # if r<80 we are looking a pixel in the blue background.    
                    #Now we turn it in black
                    (l,m,n) = (0,0,0)
                    i_trans.putpixel((x,y),(l,m,n))
     
     
     
     
        #Image.show(i_trans)            
        print("number of grain-pixel =", tot_pix_grains, "number of diseased-grain-pixel =", nb_pix_diseased, "% =", nb_pix_diseased*100/tot_pix_grains)

  4. #4
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 298
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 298
    Points : 6 778
    Points
    6 778
    Par défaut
    C'est probablement que tu as mélangé des indentations espace et tabulation

    Copie-colle tel qu'il est ici:
    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
     
    """Module ProgFDK3 including the funtion FDK_evaluation"""
     
     
    def FDK_evaluation(Name,paramB=100,paramR=79):
     
        """This function evaluate the FDK symptomes on grains
        The first variable contains the name of the picture
        The second the blue parameter which allow to recognize the diseased-grains
        and which is automatically set up to 100 for triticale
        The third variable is the red parameter which allow to recognize our blue background
        and which is automatically set up to 79"""
     
        #Load the picture
        i_orig = open(Name)
        i_trans = open(Name)
     
        #Show it
        #Image.show(i_orig)
     
        # Get the size in pixels of the picture
        (largeur, hauteur)= i_orig.size
        # initialize the number of pixel from grains and from diseased grains
        tot_pix_grains = 0
        nb_pix_diseased = 0
     
        for x in range(largeur):
            for y in range(hauteur):
                # Here we check the pixel with the coordinates(x,y) from the picture i-orig.
                # The point(0,0) is in the top left-hand corner.
                #getpixel, will get the level of red, green, and blue from the pixel
                (r,g,b) = i_orig.getpixel((x,y))
                # if r>79 we are not looking a pixel in the blue background.
                if r>79:
                    #if this pixel is not in the background it is in a grain.
                    tot_pix_grains += 1
                    # if this pixel present a level of b>100 it is turning white. it is diseased.
                    if b>100 :
                        nb_pix_diseased += 1
                        #Now we change its color
                        (l,m,n) = (204,153,255)
                        i_trans.putpixel((x,y),(l,m,n))
     
                else :
                    # if r<80 we are looking a pixel in the blue background.    
                    #Now we turn it in black
                    (l,m,n) = (0,0,0)
                    i_trans.putpixel((x,y),(l,m,n))
     
     
     
     
        #Image.show(i_trans)            
        print("number of grain-pixel =", tot_pix_grains, "number of diseased-grain-pixel =", 
                nb_pix_diseased, "% =", nb_pix_diseased*100/tot_pix_grains)
    moi j'utilise des espaces, comme ça tu sais.

    Edit: c'est plus vicieux que ça, la doc string (qui commence par trois guillemets) a une indentation de 1 espace et la ligne 14 une indentation de 4. Python n'aime pas ça.
    Moi non plus d'ailleurs.

  5. #5
    Nouveau Candidat au Club
    Femme Profil pro
    Ingénieur agronome
    Inscrit en
    Janvier 2016
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Autriche

    Informations professionnelles :
    Activité : Ingénieur agronome
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Janvier 2016
    Messages : 3
    Points : 1
    Points
    1
    Par défaut
    Merci!!

    Il faut vraiment que je télécharge un autre éditeur!

    Du coup j´ai un nouveau problème maintenant le input marche bien je peux rentrer le nom de l´image mais après avoir pris le nom la fenêtre se ferme sans afficher les résultats avec le print ...
    J´ai testé toutes les lignes de codes en les entrant directement dans le shell et ça fonctionne.

  6. #6
    Expert éminent

    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    4 298
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2008
    Messages : 4 298
    Points : 6 778
    Points
    6 778
    Par défaut
    Ouvre un terminal et entre:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    python3 FDKrunning.py
    le terminal devrait rester ouvert.

    Si tu es sous windows faudra mettre les chemins complet C:\Programmes ..... python.exe, je te laisse chercher.

Discussions similaires

  1. [Recrutement] Artiste 2D pour petit simple mais sympa
    Par LittleWhite dans le forum Projets
    Réponses: 4
    Dernier message: 12/12/2009, 20h43
  2. Premier programme sous Python
    Par Helios07 dans le forum Général Python
    Réponses: 6
    Dernier message: 06/06/2008, 11h33
  3. Réponses: 8
    Dernier message: 05/03/2008, 00h30
  4. programme simple mais besoin d'aide
    Par newvo dans le forum Windows
    Réponses: 15
    Dernier message: 07/03/2007, 17h56
  5. Petite fonction toute simple, mais
    Par renaud26 dans le forum Général JavaScript
    Réponses: 46
    Dernier message: 21/07/2006, 15h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo