Binary deployment done


#1

My hero doesnt do anything and nothing happens all that happens is goals ran out of time heres my code

def decToBin(number):
    binary = []
    while number > 0:
        if number % 2 != 0:
            binary.append(1)
        if binary % 2 == 0:
            binary.append(0)
        number = number / 2
    if number == 0 and len(binary) < 8:
        binary.append(0)
    if len(binary) == 8:
         self.say("".join(reversed(binary)))

#2

Do you have a main loop? decToBin will not be executed unless called ?

Also if len(binary)<=6, that is if number<=63 then you will never say anything


#3

so what should my code look like


#4

Im not playing code combat anymore the game has became to advanced for me and I dont know how to make vectors and all those things


#5

@lundor5 - Hi, I was having loads of trouble, but I carefully learned from your code and I think I may come upon the right function. I check the function by input of various number and checking the output. Don’t forget that ‘0’ has to be added to the start if the binary number length is less than 8. You can check if you are getting correct output or not by utilising online dec to binary converters.

Here is my code (modified from your code)
def DecimalToBinary(number):
    binary = []
    while number > 1:
        if number % 2 == 0:
            binary.append(0)
        else:
            binary.append(1)
        number = int(number/2)
    if number == 1:
        binary.append(1)
    a = 0
    if len(binary) < 9:
        a = 8 - len(binary)
        for i in range(a):
            binary.append(0)
    binary = reversed(binary)
    self.say(binary)
    
DecimalToBinary(16)