(sin asunto) (decimal en binario)

Ignazio *slack* G. lach en hotmail.it
Vie Oct 20 00:34:56 CEST 2006


def detcifra(ris):
  if ris%2 == 0:
    return "0"
  else: return "1"

def stampaStrContr(a):
  ultimo = len(a)-1
  b = ""
  while ultimo != -1:
    b += a[ultimo]
    ultimo -= 1
  print "bin: "+b

a = 0
while a!=-1:
  print "-1 para salir"
  try:
    a = input('dec: ')
    if a == -1: break
    listacif = ""
    while a > 0:
      listacif += detcifra(a)
      a = a/2
    stampaStrContr(listacif)
  except:
    print "input no valido!"

main()

Bueno esta es una version un poco larga que hice hace mucho tiempo! Espero 
que te sirva... aqui tienes la version grafica:

from Tkinter import *

class MyApp:
  def __init__(self, root):
    self.root = root

    self.message = Message(self.root)
    self.message.configure(width="3i",\
                           text="- Binary 1.5 - by Nacho\n")
    self.message.pack()

    self.frame1 = Frame(self.root)
    self.frame1.pack(ipadx="4m", ipady="1m")

    self.frame2 = Frame(self.root)
    self.frame2.pack(ipadx="4m")

    # oggetti frame 1
    self.dec = Message(self.frame1)
    self.dec.configure(text="dec: ")
    self.dec.pack(side=LEFT)

    self.inp = Entry(self.frame1)
    self.inp.pack()
    self.inp.bind("<Key-Return>", self.EVT_DEC2BIN)

    # oggetti frame 2
    self.bin = Message(self.frame2)
    self.bin.configure(text="bin: ")
    self.bin.pack(side=LEFT)

    self.out = Message(self.frame2)
    self.out.configure(width="1i",\
                       text=" ")
    self.out.pack(ipadx="1m")

  def EVT_DEC2BIN(self, evento):
    a = self.inp.get()
    a = int(a)
    listacif = ""
    while a > 0:
      listacif += self.detCifra(a)
      a = a/2
    b = self.strContr(listacif)
    self.out.configure(text=b)

  def detCifra(self, ris):
    if ris%2 == 0: return "0"
    else: return "1"

  def strContr(self, a):
    ultimo = len(a)-1
    b = ""
    while ultimo != -1:
      b+=a[ultimo]
      ultimo-=1
    return b

root = Tk()
root.title("Binary")
myapp = MyApp(root)
root.mainloop()

_________________________________________________________________
Usa il tuo PC come un telefono per chiamare cellulari e telefoni fissi! 
http://imagine-msn.com/messenger/launch80/?locale=it-it&TAB=2




Más información sobre la lista de distribución Python-es