Advertisement
Dar954826

Convertitore Basi[ITA].py

Jan 15th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. def void(var):
  2.  if var:
  3.   input("INVIO per cancellare")
  4.  print("\n"*1000)
  5. def decimale(base,numero):
  6.  lun=len(numero)-1
  7.  numero1=numero[::-1]
  8.  dec=0
  9.  for kkkkk in range(len(numero1)):
  10.   if numero1[kkkkk]=="A" and int(base) >=10:
  11.    log=10
  12.   elif numero1[kkkkk]=="B" and int(base) >=11:
  13.    log=11
  14.   elif numero1[kkkkk]=="C" and int(base) >=12:
  15.    log=12
  16.   elif numero1[kkkkk]=="D" and int(base) >=13:
  17.    log=13
  18.   elif numero1[kkkkk]=="E" and int(base) >=14:
  19.    log=14
  20.   elif numero1[kkkkk]=="F" and int(base) >=15:
  21.    log=15
  22.   elif numero1[kkkkk]=="G" and int(base) >=16:
  23.    log=16
  24.   elif numero1[kkkkk]=="H" and int(base) >=17:
  25.    log=17
  26.   elif numero1[kkkkk]=="I" and int(base) >=18:
  27.    log=18
  28.   elif numero1[kkkkk]=="J" and int(base) >=19:
  29.    log=19
  30.   elif numero1[kkkkk]=="K" and int(base) >=20:
  31.    log=20
  32.   elif numero1[kkkkk]=="L" and int(base) >=21:
  33.    log=21
  34.   elif numero1[kkkkk]=="M" and int(base) >=22:
  35.    log=22
  36.   elif numero1[kkkkk]=="N" and int(base) >=23:
  37.    log=23
  38.   elif numero1[kkkkk]=="O" and int(base) >=24:
  39.    log=24
  40.   elif numero1[kkkkk]=="P" and int(base) >=25:
  41.    log=25
  42.   elif numero1[kkkkk]=="Q" and int(base) >=26:
  43.    log=26
  44.   elif numero1[kkkkk]=="R" and int(base) >=27:
  45.    log=27
  46.   elif numero1[kkkkk]=="S" and int(base) >=28:
  47.    log=28
  48.   elif numero1[kkkkk]=="T" and int(base) >=29:
  49.    log=29
  50.   elif numero1[kkkkk]=="U" and int(base) >=30:
  51.    log=30
  52.   elif numero1[kkkkk]=="V" and int(base) >=31:
  53.    log=31
  54.   elif numero1[kkkkk]=="W" and int(base) >=32:
  55.    log=32
  56.   elif numero1[kkkkk]=="X" and int(base) >=33:
  57.    log=33
  58.   elif numero1[kkkkk]=="Y" and int(base) >=34:
  59.    log=34
  60.   elif numero1[kkkkk]=="Z" and int(base) >=35:
  61.    log=35
  62.   else:
  63.    log=numero1[kkkkk]
  64.   if str(log).count("0")==0 and str(log).count("1")==0 and str(log).count("2")==0 and str(log).count("3")==0 and str(log).count("4")==0 and str(log).count("5")==0 and str(log).count("6")==0 and  str(log).count("7")==0 and str(log).count("8")==0 and str(log).count("9")==0:
  65.    try:
  66.     6/0
  67.    except ZeroDivisionError:
  68.     print("Impossibile utilizzare il numero, cambiare base o cambiare numero")
  69.     void(True)
  70.     base()
  71.   dec=dec+int(log)*(int(base)**kkkkk)
  72.  print("Decimale = "+str(dec))
  73.  binario(dec)
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. def binario(a):
  83.  d=e=l=""
  84.  c=int(a)
  85.  while True:
  86.     if c==0:
  87.      break
  88.     else:
  89.      l=c%2
  90.      c=c//2
  91.      d=d+str(l)
  92.  e=d[::-1]
  93.  print("Binario = "+e)
  94.  esadecimale(e)
  95. def esadecimale(e):
  96.  import re
  97.  num={"0000":"0","1000":"1","0100":"2","1100":"3","0010":"4","1010":"5","0110":"6","1110":"7",
  98.       "0001":"8","1001":"9","0101":"A","1101":"B","0011":"C","1011":"D","0111":"E","1111":"F"}
  99.  x=""
  100.  o="000"
  101.  oe=o+e
  102.  oe=oe[::-1]
  103.  f=re.findall("....",oe)
  104.  for lll in range(len(f)):
  105.   k=f[lll]
  106.   x=x+num[k]
  107.  x=x[::-1]
  108.  print("Esadecimale = "+x)
  109.  ottale(e)
  110.  
  111. def ottale(e):
  112.  import re
  113.  num={"000":"0","100":"1","010":"2","110":"3","001":"4","101":"5","011":"6","111":"7"}
  114.  x=""
  115.  o="00"
  116.  he=o+e
  117.  he=he[::-1]
  118.  f=re.findall("...",he)
  119.  for lll in range(len(f)):
  120.   k=f[lll]
  121.   x=x+num[k]
  122.  x=x[::-1]
  123.  print("Ottale = "+x)
  124.  void(True)
  125. def base():
  126.  try:
  127.   import string
  128.   void(False)
  129.   d=e=l=""
  130.   a=input("Inserire il numero da convertire: ")
  131.   b=input("Inserire base numero: ")
  132.   a=a.upper()
  133.   for kjkj in string.ascii_letters:
  134.    if b.count(kjkj)!=0 or b=="" or a=="":
  135.     6/0
  136.   for kjkjk in string.punctuation:
  137.    if a.count(kjkjk)!=0 :
  138.     6/0
  139.    if b.count(kjkjk)!=0:
  140.     6/0
  141.   decimale(b,a)
  142.  except ZeroDivisionError:
  143.   print("Utilizzare caratteri validi.")
  144.   void(True)
  145. while True:
  146.  base()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement