[SOLVED] Help on Binary Deployment

I will return to you later. Bye!

Andrei

2 Likes

This post is supposed to stop the closure of this topic.

Andrei

2 Likes

Auto-closure fixed…for now.

2 Likes

Thank you for your advice I will try it and see if it works.

1 Like

ok didn’t work new code though cause my bro made it and is trying to help me.

def add0s(on):
    if len(on) < 8:
        while len(on) < 8:
            on = "0" + on
    return on
def toTernary(number):
    # Start with an empty string.
    string = ""
    # Then, while the number isn't zero:
    while number != 0:
        # We grab the remainder of our number.
        remainder = number % 3
        # This is our iterator method. 'number' decrements here.
        number = (number - remainder) / 3
        # Append the string to the remainder.
        string = remainder + " " + string
    # Finally we want to return our constructed string.
    return string
    
def toBinary(number):
    string = ""
    # Go through the steps again:
        # Get remainder, decrement, and append string.
    # Remember that binary is another way of saying '2'!
    while number != 0:
        # We grab the remainder of our number.
        remainder = number % 2
        # This is our iterator method. 'number' decrements here.
        number = (number - remainder) / 2
        # Append the string to the remainder.
        string = remainder + " " + string
    # Finally we want to return our constructed string.
    
    return string
while True:
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "warlock":
            des = friend.deployment
            iss = toTernary(des)
            for idd in iss:
                if idd == "0":
                    hero.summon("soldier")
                if idd == "1":
                    hero.summon("archer")
                if idd == "2":
                    hero.summon("griffin-rider")
        if friend.type == "paladin":
            dep = friend.deployment
            summ = toBinary(dep)
            on = add0s(summ)
            for od in on:
                if od == "0":
                    hero.summon("soldier")
                if od == "1":
                    hero.summon("archer")
    
for friend in friends:
    xArr = [50, 45, 40, 35, 30, 25, 20, 15]
    for x in xArr:
        if friend.type != "warlock" and friend.type != "paladin":
            
            hero.command(friend, "move", {'x': x, 'y': y})

I get an error saying that I need an object in line 11

does the add0s code look like it will work because it is not working for me

def add0s(summ):
    if len(summ) < 8:
        while len(summ) < 8:
            summ = "0" + summ
            pass
        return summ
        pass
    else:
        return summ

I haven’t extensively tested this but I think the problem with that function is that you’re trying to mix numbers and strings. You inputted a number and you’re trying to append a string onto it.
You’d have to do something like:
First you need two functions which aren’t used in CoCo: int() and str(). They turn whatever’s inside the brackets into either an integer or a string. (you cant turn text into an integer).
e.g.

string = '0'
integer = 999
sum = string + str(integer) # this will be '0999'. Remember it's a string! 
# If you put int() before sum now, it will get rid of the 0 because 0999 isn't a proper number, but you want to keep it as a string.

I’m not exactly sure what you’re tactic is, but I hope this helps you work it out.
Danny

2 Likes

ok I figured it out some It was not running the if statement so I managed to get it to do that now I need it to actually add the 0s thanks for the help @Deadpool198 that was the problem. FYI I just needed to add this line:

summ = [summ]

to get the code to run.

1 Like

Have you completed the level @tv337?

Andrei

2 Likes

no I need to get the code to only add a 0 if it needs to

Ok, can you show us your new code?

Andrei

1 Like
def add0s(z):
    
    
    if len(z) < 8:
        while len(summ) < 8:
            z = 0 + z
            pass
        return z
        pass
    else:
        return z
        pass

I meant can you show us your whole new code?

Andrei

1 Like

sorry I just finished it thanks for all yall’s help.

No problem! Anytime! And congratulations for completing the level! :partying_face:

Andrei

1 Like