The gosh darn sample code is KAPUT 😭 [SOLVED]

image
this is a sample code from hashing magic
it always returns 0

You changed the sample code a little bit, which broke it. It was this:

# Use the hash function to find places for soldiers.
totalCells = 135
# This function hashes a string value to an integer.
def hashName(name):
    hash = 0
    for i in range(len(name)):
        letter = name[i]
        hash += ord(letter)
    # The hash value should be from 0 to 134.
    # Use modulo operation to "cut" the hash value.
    
    return hash
soldiers = hero.findByType("soldier")
# For each soldier say his/her name (id) and hash of it.
# The name and the number should be splitted by whitespace.

The part you changed doesn’t work because ord() requires a letter, not a number, which is what the variable “letter” becomes.

for letter in range (len(name)):
    hero.say(letter)
    hash += ord(letter)

After it the code goes through hero.say(letter) in bugs out because ord() is being used incorrectly.

the code doesn’t work because the for statement doesn’t run. I debuged using hero.say() and it didn’t work.

sorry nvm i fixed it

1 Like