Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Bits and Trits Help - Python

What my code does is give the robot some numbers, and it says, " there is no enemies with type: #". Please help.

# Incoming Ogre Brawlers!
# Make use of a Robot Walker to dispatch these enemies.
# The Robot Walker requires commands as strings.
# The first part will the enemy's health in ternary.
# The second part will be the enemy's type as binary.

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:
        remainder = number % 3
        number = (number - remainder) / 3
        string = remainder + string
    return string

while True:
    enemies = hero.findEnemies()
    dangerous = findMostDangerous(enemies)
    if dangerous:
        # The way the robot takes commands is in the form of:
        # ternary(enemyHealth) + " " + binary(enemyType)
        hero.say(toTernary(dangerous.health) + " " + toBinary(dangerous.type))

# In this level the Ogre Brawlers are more powerful if they have more health.
def findMostDangerous(enemies):
    mostDangerous = None
    mostHealth = 0
    for i in range(len(enemies)):
        enemy = enemies[i]
        if enemy.health > mostHealth:
            mostDangerous = enemy
            mostHealth = enemy.health
    return mostDangerous

In your toBinary function, take a closer look at the comment on line 25. Then, have a look at your code…one thing should stand out.

The comment says that binary is another way of saying 2. Do I just go with

hero.say("2")

???

Well, not quite. Notice that in the toTernary function, it is dividing by 3?:

        remainder = number % 3
        # This is our iterator method. 'number' decrements here.
        number = (number - remainder) / 3

Ternary means a number system based on 3 digits…0, 1, 2. That code is dividing by 3, which is part of the method to convert ‘number’ the ternary system.

Binary on the other hand is a number system based on 2 digits…0, 1. Since 2 is needed to convert ‘number’ to the binary system, try dividing by 2…not 3…in the toBinary function.

It didn’t work for me.

hmm…post your code again please.

ok.

# Incoming Ogre Brawlers!
# Make use of a Robot Walker to dispatch these enemies.
# The Robot Walker requires commands as strings.
# The first part will the enemy's health in ternary.
# The second part will be the enemy's type as binary.

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:
        remainder = number % 3
        number = (number - remainder) / 2
        string = remainder + string
    return string

# In this level, the Ogre Brawlers are stronger if they have more health.
def findMostDangerous(enemies):
    mostDangerous = None
    mostHealth = 0
    for i in range(len(enemies)):
        enemy = enemies[i]
        if enemy.health > mostHealth:
            mostDangerous = enemy
            mostHealth = enemy.health
    return mostDangerous
    
    
while True:
    enemies = hero.findEnemies()
    dangerous = findMostDangerous(enemies)
    if dangerous:
        # The way the robot takes commands is in the form of:
        # ternary(enemyHealth) + " " + binary(enemyType)
        hero.say(toTernary(dangerous.health) + " " + toBinary(dangerous.type))

Almost!..you need to change the modulo too.

What is the modulo???

You still have a 3 in that function…change % 3

Thank you so much!!!