Scrabble Score, Recursive problem


#1

OK, so I decided to try and rebuild my anti-vowel using the structure you mentioned, but I had one big problem:

How do I iterate up the list without a counter?

So I had my structure all set up, but didn't know how to iterate it. Decided to look at your link and saw the text[1:]

So that's how you do it.

Should make things simpler now. Thanks mate!

On the side, here's my scrabble score recursive function. Unfortuantely, I cant decide on a good base case, perhaps you can help me @datfatcat?

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
         "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
         "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
         "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
         "x": 8, "z": 10}
         
def scrabble_score(word):
    word = word.lower()
    word_length = len(word) 
    if len(word) == 0:
        return word_length
    else:
        return scrabble_score(word[1:]) + score[word[0]]


print scrabble_score("Death")

Anti-vowel, Recursive problem(SOLVED)
#2

Hi @rejor11,

A good base case would be where word is an empty string, which is what you essentially have already.

You do not need to use the len function; this is simpler ...

    if word == "":
        return 0

#3

This is my code. Looks like everything is working. I am getting a right answers as well but don't know what this error is. Can anyone please help solve this issue?

def scrabble_score(word):
word = word.lower()
total = 0
for n in word:
total += score[n]
return total
print scrabble_score("apple")

Oops, try again. Your function crashed on pie as input because your function throws a "'int' object is not callable" error.


#4

you dont need the += you just need to do
total = score[n] + total


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.