bonjour à tous,
voila j'ai une variable qui me fait une erreur, je cherche depuis un moment mais je ne comprend pas pourquoi.
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 
 
def alea():
	import random
	r = random.randint(1,6)
	return r
 
def info(x):
        if x==1 :
                te="banane"
        if x==2 :
                te="pomme"
        if x==3 :
                te="poire"
        if x==4 :
                te="BAR"
        if x==5 :
                te="cerise"
        if x==6 :
                te="ananas"        
        return te
 
def gain(un,de,tr,gain,somme):
        if un==1 and de==1 and tr==1:
                mul=2
        elif un==1 and de==1:
                mul=1.1
        elif un==1 and tr==1:
                mul=1.1
        elif de==1 and tr==1:
                mul=1.1
 
        elif un==2 and de==2 and tr==2:
                mul=3
        elif un==2 and de==2:
                mul=1.2
        elif un==2 and tr==2:
                mul=1.2
        elif de==2 and tr==2:
                mul=1.2
 
        elif un==3 and de==3 and tr==3:
                mul=4
        elif un==3 and de==3:
                mul=1.3
        elif un==3 and tr==3:
                mul=1.3
        elif de==3 and tr==3:
                mul=1.3
 
        elif un==4 and de==4 and tr==4:
                mul=5
        elif un==4 and de==4:
                mul=1.4
        elif un==4 and tr==4:
                mul=1.4
        elif de==4 and tr==4:
                mul=1.4
 
        elif un==5 and de==5 and tr==5:
                mul=6
        elif un==5 and de==5:
                mul=1.5
        elif un==5 and tr==5:
                mul=1.5
        elif de==5 and tr==5:
                mul=1.5
 
        elif un==6 and de==6 and tr==6:
                mul=7
        elif un==6 and de==6:
                mul=1.6
        elif un==6 and tr==6:
                mul=1.6
        elif de==6 and tr==6:
                mul=1.6           
        else :
                mul=-1
 
        somme=int(somme+gain*mul)
 
        return somme        
 
def start_it():
 
        "démarrage de l'animation"
        pari =int(entr1.get())
 
        a=alea()
        b=alea()
        c=alea()
 
        d=info(int(a))
        e=info(int(b))
        f=info(int(c))
 
        txt4.configure(text = d)
        txt5.configure(text = e)
        txt6.configure(text = f)
 
 
        argent = gain(a,b,c,pari,argent)
 
        txt7.configure(text = 'il vous argent '+argent+' euros')
 
 
 
import os
from tkinter import *
 
argent = 100
 
pari = 0
e=d=f="vide"
 
toto = 'il vous argent ',argent,' euros'
fen1 = Tk()
# création de widgets 'Label' et 'Entry' :
txt1 = Label(fen1, text ='bienvenue, vous avez')
txt2 = Label(fen1, text =argent)
txt3 = Label(fen1, text ='euro pour jouer')
txt4 = Label(fen1, text =e)
txt5 = Label(fen1, text =d)
txt6 = Label(fen1, text =f)
txt7 = Label(fen1, text =toto)
entr1 = Entry(fen1)
 
 
bou2 = Button(fen1, text='Démarrer', width =8, command=start_it)
bou2.pack()
 
 
txt1.grid(row =1, column =1)
txt2.grid(row =1, column =2)
txt3.grid(row =1, column =3)
 
txt4.grid(row =3, column =1)
txt5.grid(row =3, column =2)
txt6.grid(row =3, column =3)
 
txt7.grid(row =4, column =2)
 
entr1.grid(row =2, column =2)
 
bou2.grid(row =2, column =3)
fen1.mainloop()
bon en résumé c'est un petit jeux à la con qui me permet de faire divers manipulation pour apprendre. il s'agit d'un jeu de machine à sous très simplifié.
Le programme démarre bien, il me trace bien la fenêtre comme je veux. je rentre une valeur dans la case de saisi, cette valeur correspond à la mise de départ.
Le programme me récupère bien la valeur, le texte se modifie bien mais à un moment j'ai une erreur qui me sort:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1489, in __call__
    return self.func(*args)
  File "C:/Users/cyril.cagnacci/Desktop/python/code fonctionnelle/jackpot1.py", line 102, in start_it
    argent = gain(a,b,c,pari,argent)
UnboundLocalError: local variable 'argent' referenced before assignment
cette erreur viens du sous programme appelé par le bouton et plus particulièrement de l'appel de ma sous fonction de calcul de gain.
Le résultat ne se copie dans dans la variable argent car il dit que cette variable n'est pas déclaré, sauf que là ou je ne comprend pas c'est que le problème ne viens que de cette variable. Si je modifie mon code de:
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
def start_it():
 
        "démarrage de l'animation"
        pari =int(entr1.get())
 
        a=alea()
        b=alea()
        c=alea()
 
        d=info(int(a))
        e=info(int(b))
        f=info(int(c))
 
        txt4.configure(text = d)
        txt5.configure(text = e)
        txt6.configure(text = f)
 
 
        argent = gain(a,b,c,pari,argent)
 
        txt7.configure(text = 'il vous argent '+argent+' euros')
à ca :

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
def start_it():
 
        "démarrage de l'animation"
        pari =int(entr1.get())
 
        a=alea()
        b=alea()
        c=alea()
 
        d=info(int(a))
        e=info(int(b))
        f=info(int(c))
 
        txt4.configure(text = d)
        txt5.configure(text = e)
        txt6.configure(text = f)         
 
        txt7.configure(text = 'il vous argent '+gain(a,b,c,pari,argent)+' euros')
bein je n'ai pas d'erreur généré et ma variable argent qui est le reste après pari ne se mets jamais à jour.

j'ai surement fait quelque chose de mal mais je ne vois pas quoi, pouvez vous m'aider svp?
merci d'avance