Probleme "Calculatrice"

Résolu
elnabo Messages postés 28 Date d'inscription vendredi 7 août 2009 Statut Membre Dernière intervention 2 mai 2016 - 31 août 2009 à 10:53
elnabo Messages postés 28 Date d'inscription vendredi 7 août 2009 Statut Membre Dernière intervention 2 mai 2016 - 1 sept. 2009 à 09:24
Bonjour tout le monde.

Comme certain l'ont déja vu , j'ai coder une petite calculatrice avec un code très basique , qui est donc assez long.

J'ai l'intention d'en faire un programme qui me suivra au cours des mes apprentissages (d'abord celui de G S) du python, pour qu'il se rapproche du lambda puis bah il sera peut-être en lambda après.

En attendant, j'ai voulu rajouter la definition nombre pour supprimer un max de ligne. Seulement , il y a un problème avec cette fonction qui ne marche que pour les nombres avant la virgule. Elle afficher 120 sans problème mais refusera d'écrire 3.201 alors que si j'écris directement la fonction nombre sous la fonction x=3 par exemple je peux écire 3.3

J'ai mis print n dans cette fonction x=0 et donc je vois qu'il y a un problème au niveau de l'incrémentation de la variable n
Je crois que c'est juste un probleme qui fait que la valeur de n à l'origine n'est pas supprimé, ce qui fait que n vaut toujouts 1 à chaque fois que l'on lance la fonction nombre(x,n)

Si vous pouviez m'aider à arranger ça je vous en serez eternellement reconnaissant

[size=70]
# -*- coding: cp1252 -*- 
from Tkinter import*
import Tkinter as Tk
from math import *
t,y,p,n=0,0,0,1
def nombre(x,n):
        global t
        if p==0:
                t=t*10+x
        else:
                t=t+x/(10**(n))
                n=n+1
        z.set(str(t))                
def x0():
        nombre(0,n)
        print n
def x1():
        nombre(1,n)       
def x2():
        nombre(2,n)               
def x3():
        global x,t,p,n
        x=3.
        if p==0:
                t=t*10+x
        else:
                t=t+x/(10**(n))
                n=n+1
        z.set(str(t))
def x4():
        global x,t,p,n
        x=4.
        if p==0:
                t=t*10+x
        else:
                t=t+x/(10**(n))
                n=n+1
        z.set(str(t))
def x5():
        global x,t,p,n
        x=5.
        if p==0:
                t=t*10+x
        else:
                t=t+x/(10**(n))
                n=n+1
        z.set(str(t))
def x6():
        global x,t,p,n
        x=6.
        if p==0:
                t=t*10+x
        else:
                t=t+x/(10**(n))
                n=n+1
        z.set(str(t))
def x7():
        global x,t,p,n
        x=7.
        if p==0:
                t=t*10+x
        else:
                t=t+x/(10**(n))
                n=n+1
        z.set(str(t))
def x8():
        global x,t,p,n
        x=8.
        if p==0:
                t=t*10+x
        else:
                t=t+x/(10**(n))
                n=n+1
        z.set(str(t))
def x9():
        global x,t,p,n
        x=9.
        if p==0:
                t=t*10+x
        else:
                t=t+x/(10**(n))
                n=n+1
        z.set(str(t))
def point():
        global p
        p = '.'
def aplus():
        global y,t1,t,p,n
        y,t1,p,n = '+',t,0,1
        t=0               
def amoins():
        global y,t1,t,p,n
        y,t1,p,n = '-',t,0,1
        t=0                        
def afois():
        global y,t1,t,p,n
        y ,t1,p,n= '*',t,0,1
        t=0
def adiv():
        global y,t1,t,p,n
        y,p,t1,n='/',0,t,1
        t=0                      
def aegal():
        global t,t1,n,p
        v ,p= '=',0
        if y == '+':
                z.set(str((t1+t)))
                t=t1+t
        elif y== '-':
                z.set(str((t1-t)))
                t=t1-t
        elif y== '*':
                z.set(str((t1*t)))
                t=t1*t
        elif y=='/':
                z.set(str((t1/t)))
                t=t1/t
        t1,n=0,1
def clear():
        global t,t1,n,p,z,y
        t,t1,n=0,0,0
        p,y=0,0
        z.set(str('0'))
        
#-----Prog Principal-----#

fen = Tk.Tk()
fen.title('Calculatrice')
fra1 = Frame(fen)
fra1.grid(row=1,column=0)
Button(fra1, text = '9', command= x9).grid(row=2, column = 2, padx = 3, pady = 3)
Button(fra1, text = '8', command= x8).grid(row=2, column = 1, padx = 3, pady = 3)
Button(fra1, text = '7', command= x7).grid(row=2, column = 0, padx = 3, pady = 3)
Button(fra1, text = '6', command= x6).grid(row=3, column = 2, padx = 3, pady = 3)
Button(fra1, text = '5', command= x5).grid(row=3, column = 1, padx = 3, pady = 3)
Button(fra1, text = '4', command= x4).grid(row=3, column = 0, padx = 3, pady = 3)
Button(fra1, text = '3', command= x3).grid(row=4, column = 2, padx = 3, pady = 3)
Button(fra1, text = '2', command= x2).grid(row=4, column = 1, padx = 3, pady = 3)
Button(fra1, text = '1', command= x1).grid(row=4, column = 0, padx = 3, pady = 3)
Button(fra1, text = '0', command= x0).grid(row=5, column = 2, padx = 3, pady = 3)

z = StringVar()
entree=Entry(fen,textvariable=z)
entree.grid(row=0,column=0)
z.set("0.")

Button(fra1, text'+', command aplus).grid(row=2,column=5, padx = 3, pady = 3)
Button(fra1, text'-', command amoins).grid(row=3,column=5, padx = 3, pady = 3)
Button(fra1, text'*', command afois).grid(row=2,column=6, padx = 3, pady = 3)
Button(fra1, text'/', command adiv).grid(row=3,column=6, padx = 3, pady = 3)
Button(fra1, text'.', command point).grid(row=4,column=5, padx = 3, pady = 3)
Button(fra1, text= '=', command = aegal).grid(row=4,column=6, padx = 3, pady = 3)
Button(fra1, text'C', command clear).grid(row=5, column=6, padx = 3, pady = 3)
Button(fen,text='Quitter',command = fen.quit).grid(row=6,column=7)
fen.mainloop

1 réponse

elnabo Messages postés 28 Date d'inscription vendredi 7 août 2009 Statut Membre Dernière intervention 2 mai 2016
1 sept. 2009 à 09:24
Probleme résolu, il suffisait en fait, de mettre la valeur n en global dans def nombre(x), ce que j'avais déja essayé.


Mais surtout, il fallait mettre un point après les chiffres , le truc tout bete que j'avais oublié :'(
3
Rejoignez-nous