Local variable "scoring" not defined


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/3?curriculum_id=4f89dab3d788890003000096#

I don't get this...
Does my code look okay? I run it and it says, "Oops, try again. Did you create a function called scrabble_score? Your code threw a "local variable 'scoring' referenced before assignment" error."
What's wrong?
Oh, and by the way, I don't have an attitude. I just picked this username because I couldn't pick anything else.


def scrabble_score(word):
    for char in word:
        scoring == 0
        scoring += score[char.lower()]


#2

Why compare(==), why not assign (=)? Also, take that line outside the for loop.

You should also return scoring

def scrabble_score(word):
	scoring = 0
	for char in word:
		scoring += score[char.lower()]
	return scoring

Does this help?


#3

I just tried your code and it didn't work.


#4

Error:

Oops, try again. Your function fails on scrabble_score("pie"). It returns "None" when it should return "5".


#5

Err... I just edited. Please check again.

Thank you.

def scrabble_score(word):
	scoring = 0
	for char in word:
		scoring += score[char.lower()]
	return scoring

#6

From my end, it works.

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):
	scoring = 0
	for char in word:
		scoring += score[char.lower()]
	return scoring
print scrabble_score("MAGIC")

I got an output of 10 which is right.


#7

hum

Thats weird.

What computer do you have?


#8

If u dont want to say you dont have to


#9

Haha... Please post full code and well formatted.

Thank you.


#10

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):
scoring = 0
for char in word:
scoring += score[char.lower()]


#11

Now, you didn't follow through. I said to return scoring.

More directly, please copy and paste the below:

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):
	scoring = 0
	for char in word:
		scoring += score[char.lower()]
	return scoring
print scrabble_score("MAGIC")

PLEASE MIND INDENTATION LEVELS TOO.

Hope this resolves! :slight_smile: