Python Forum
Decimal to binary weighted number converter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decimal to binary weighted number converter
#1
Hi, I'm Learning more about lists and loops, to be able to make my own
projects as a hobby. My code is very amaturish, I'm not a coder by
profession, please excuse the elementary scripting.
Thanks.
Here is a program I developed using my understanding of nested loops,
it is a converter from decimal value to a binary weighted output.
Insted of 0ne's and zero's, it shows the binary weight of the decimal number.
It's only good up to 8 bits for the output answer. If the output requires more
than 8 bits to show the binary weight of the decimal number, then the computer
will return an empty value. There may be minor bugs in it, if you use this program,
please comment on improvements to make it more user friendly.
As far as the coding, I'm still at the elementary level, so any suggestions on coding,
may be way above me to comprehend for right now.
enjoy:


import os
class klass:
    "docstring"
    def_int-self:
       self.attribute="var"
       if len (self.attribute)<2:
           pass


print("");print("")
play=0
while play==0:
    listrev=[];bnwt=[];listgather=[]
    print ("  Enter the amount of bits to make a list for")
    print ("      accuracy only works up to (8) bits")
    print ("   If you choose a number higher than <<(8)>>")
    print ("Then the accuracy of the output is NOT guaranteed!")
    print("");print("      Enter (0) to exit the program")
    cnt=0
    while cnt==0:
        try:
            print("")
            userstart=input("Enter a number >>  ");userstart=int(userstart)
            if userstart >(8):
                print("")
                print ("The accuracy of this only goes up to (8) bits!")
                print ("You chose (",(userstart),") bits for your list")
                print ("There is no guarantee, the output will be correct")
                print("")
                print("If the output shows up with duplicate numbers, the answer in NOT legit.")
                print ("             still continue with number (",(userstart),")?")
                print("")
                useragain=input ("continue with this number, enter (y)for yes or (n) for no>>  ")
                if useragain=="y":
                    print("")
                    cnt=1
            elif userstart==0:
                break
                
            else:
                cnt=1
        except:
            print("")
            print ("Enter a valid number>>  ")
    if userstart==0:
        break
    print("")
    n=1
    for item in range(userstart):
        listrev.append(n)
        n=(n*2)
    n=-1
    for item in (listrev):
        bnum=(listrev[n])
        bnwt.append(bnum)
        n=(n-1)
    chk=0
    while chk==0:
        try:
            user=input("enter the number to convert to binary weighted number >> ");user=int(user)
            chk=1
        except:
            print("")
            print("Enter a number only")
            print("")
    print("")
    print("Please wait while Calculating")
    flg=0
    if flg==0:
        for a in (bnwt):
                if (a)==(user):
                    listgather.append(a)
                    flg=1 
                #print("A")


    if flg==0:
        for a in (bnwt):
            for b in (bnwt):
                if ((a)+(b))==(user)and ((a)!=(b)):
                    listgather.append(a)
                    listgather.append(b)
                    flg=2
                #print ("B")
                #z=((a)+(b))
                #print (z)
                    
    if flg==0:            
        for a in (bnwt):
            for b in (bnwt):
                for c in (bnwt):
                    if ((a)+(b)+(c))==(user)and ((a)!=(b))and ((b)!=(c))and ((a)!=(c)):
                        listgather.append(a)
                        listgather.append(b)
                        listgather.append(c)
                        flg=3
                    #print ("C")
                    #z=((a)+(b)+(c))
                    #print (z)
                        
    if flg==0:            
        for a in (bnwt):
            for b in (bnwt):
                for c in (bnwt):
                   for d in (bnwt):
                       if ((a)+(b)+(c)+(d))==(user)and((a)!=(b))and ((b)!=(c))and ((c)!=(d)):
                           if ((a)!=(c))and ((a)!=(d))and ((b)!=(d)):
                               listgather.append(a)
                               listgather.append(b)
                               listgather.append(c)
                               listgather.append(d)
                               flg=4
                       #print ("D")
                           #z=((a)+(b)+(c)+(d))
                           #print (z)
                           
    if flg==0:            
        for a in (bnwt):
            for b in (bnwt):
                for c in (bnwt):
                   for d in (bnwt):
                       for e in (bnwt):
                           if ((a)+(b)+(c)+(d)+(e))==(user)and((a)!=(b))and ((b)!=(c))and ((c)!=(d)):
                               if((d)!=(e))and ((a)!=(c))and((a)!=(d))and ((a)!=(e))and((b)!=(d)): 
                                   if((b)!=(e))and ((c)!=(e)):                                               
                                       listgather.append(a)
                                       listgather.append(b)
                                       listgather.append(c)
                                       listgather.append(d)
                                       listgather.append(e)
                                       flg=5
                          #print("E")
                              #z=((a)+(b)+(c)+(d)+(e))
                              #print (z)
                               
                               
    if flg==0:            
        for a in (bnwt):
            for b in (bnwt):
                for c in (bnwt):
                   for d in (bnwt):
                       for e in (bnwt):
                          for f in (bnwt): 
                               if ((a)+(b)+(c)+(d)+(e)+(f))==(user)and((a)!=(b))and ((b)!=(c))and ((c)!=(d)):
                                   if ((d)!=(e))and ((a)!=(c))and((a)!=(d))and ((a)!=(e))and((b)!=(d))and((b)!=(e)):
                                      if((c)!=(e))and ((a)!=(f))and((b)!=(f))and ((c)!=(f))and((d)!=(f))and((e)!=(f)): 
                                          listgather.append(a)
                                          listgather.append(b)
                                          listgather.append(c)
                                          listgather.append(d)
                                          listgather.append(e)
                                          listgather.append(f)
                                          flg=6
                               #print("F")
                               #z=((a)+(b)+(c)+(d)+(e))
                               #print (z)
                                   
    if flg==0:            
        for a in (bnwt):
            for b in (bnwt):
                for c in (bnwt):
                   for d in (bnwt):
                       for e in (bnwt):
                           for f in (bnwt):
                               for g in (bnwt):
                                   if ((a)+(b)+(c)+(d)+(e)+(f)+(g))==(user)and((a)!=(b))and ((b)!=(c))and ((c)!=(d)):
                                       if ((d)!=(e))and ((a)!=(c))and((a)!=(d))and ((a)!=(e))and((b)!=(d))and((b)!=(e)):
                                          if((c)!=(e))and ((a)!=(f))and((b)!=(f))and ((c)!=(f))and((d)!=(f))and((e)!=(f)):
                                              if ((a)!=(g))and ((b)!=(g))and((c)!=(g))and ((d)!=(g))and ((e)!=(g))and ((f)!=(g)): 
                                                  listgather.append(a)
                                                  listgather.append(b)
                                                  listgather.append(c)
                                                  listgather.append(d)
                                                  listgather.append(e)
                                                  listgather.append(f)
                                                  listgather.append(g)
                                                  flg=7
                                   #print("G")
                                   #z=((a)+(b)+(c)+(d)+(e))
                                   #print (z)
                                       
    if flg==0:            
        for a in (bnwt):
            for b in (bnwt):
                for c in (bnwt):
                   for d in (bnwt):
                       for e in (bnwt):
                          for f in (bnwt):
                             for g in (bnwt):
                                 for h in (bnwt):
                                       if ((a)+(b)+(c)+(d)+(e)+(f)+(g)+(h))==(user)and((a)!=(b))and ((b)!=(c))and ((c)!=(d)):
                                          if ((d)!=(e))and ((a)!=(c))and((a)!=(d))and ((a)!=(e))and((b)!=(d))and((b)!=(e)):
                                              if((c)!=(e))and ((a)!=(f))and((b)!=(f))and ((c)!=(f))and((d)!=(f))and((e)!=(f)):
                                                  if ((a)!=(g))and ((b)!=(g))and((c)!=(g))and ((d)!=(g))and ((e)!=(g))and ((f)!=(g)):
                                                      if ((a)!=(h))and((b)!=(h))and((c)!=(h))and ((d)!=(h))and ((e)!=(h))and((f)!=(h))and ((g)!=(h)):
                                                          listgather.append(a)
                                                          listgather.append(b)
                                                          listgather.append(c)
                                                          listgather.append(d)
                                                          listgather.append(e)
                                                          listgather.append(f)
                                                          listgather.append(g)
                                                          listgather.append(h) 
                                                          flg=8
                                       #print ("H")  
                                       #z=((a)+(b)+(c)+(d)+(e)+(f)+(g)+(h))
                                       #print (z)
    
    listgather2=[]
##    for y in (bnwt):
##        flag=0
##        for z in (listgather[0:x]):
##            if (y)==(z):
##                listgather2.append(z)
##                flag=1
##        if flag==0:
##            listgather2.append ("0")
    print("")
    user=str(user)
    if flg==0:
        print("Here are the binary weighted values that add up to equal "+"("+(user)+")")
        print("")
        print ("                      No output to show")
        print ("The answer requires more than (8) bits to compute the total output")
    print("")
    print("")
    #print (listgather)
    if flg==1:
        for y in (bnwt):
            flag=0
            for z in (listgather):
                if (y)==(z):
                    listgather2.append(z)
                    flag=1
            if flag==0:
                listgather2.append ("0") 
        print ("                ",(listgather2[0:(len(bnwt))]))
    if flg==2:
        for y in (bnwt):
            flag=0
            for z in (listgather[0:2]):
                if (y)==(z):
                    listgather2.append(z)
                    flag=1
            if flag==0:
                listgather2.append ("0")  
        print ("                ",(listgather2[0:(len(bnwt))]))
    if flg==3:
        for y in (bnwt):
            flag=0
            for z in (listgather[0:3]):
                if (y)==(z):
                    listgather2.append(z)
                    flag=1
            if flag==0:
                listgather2.append ("0")
 
        print ("                ",(listgather2[0:(len(bnwt))]))
    if flg==4:
        for y in (bnwt):
            flag=0
            for z in (listgather[0:4]):
                if (y)==(z):
                    listgather2.append(z)
                    flag=1
            if flag==0:
                listgather2.append ("0")

        print ("                ",(listgather2[0:(len(bnwt))]))
    if flg==5:
        for y in (bnwt):
            flag=0
            for z in (listgather[0:5]):
                if (y)==(z):
                    listgather2.append(z)
                    flag=1
            if flag==0:
                listgather2.append ("0")
 
        print ("                ",(listgather2[0:(len(bnwt))]))
    if flg==6:
        for y in (bnwt):
            flag=0
            for z in (listgather[0:6]):
                if (y)==(z):
                    listgather2.append(z)
                    flag=1
            if flag==0:
                listgather2.append ("0")

        print ("                ",(listgather2[0:(len(bnwt))]))
    if flg==7:
        for y in (bnwt):
            flag=0
            for z in (listgather[0:7]):
                if (y)==(z):
                    listgather2.append(z)
                    flag=1
            if flag==0:
                listgather2.append ("0")
 
        print ("                ",(listgather2[0:(len(bnwt))]))
    if flg==8:
        for y in (bnwt):
            flag=0
            for z in (listgather[0:8]):
                if (y)==(z):
                    listgather2.append(z)
                    flag=1
            if flag==0:
                listgather2.append ("0")
 
        print ("                ",(listgather2[0:(len(bnwt))])) 
    print("")
    user=input("Play again (y) or (n) >>  ")
    print("");print("");print("")
    if user =="n":
        play=1
    else:
        print("")
user=input ("Hit the enter key to end ")
Reply
#2
>>> "{0:b}".format(112)
'1110000'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  propositional logic formula converter bobsmith76 8 4,944 Dec-04-2018, 02:54 AM
Last Post: bobsmith76

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020