Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

On ps3, problem number 1 I keep recieving the error message: >>> get_word_score(cat, 5) Traceback (most recent call last): File "", line 1, in get_word_score(cat, 5) NameError: name 'cat' is not defined I even get the error message when I use the answer key... Can anybody help?

OpenStudy (anonymous):

def get_word_score(word, n)

OpenStudy (anonymous):

The code is def get_word_score(word, n): nf = int(n) lenword = int(len(word)) ws = 0 for i in range(0,lenword): ws = ws + int(SCRABBLE_LETTER_VALUES[word[i]]) if lenword == n: ws = ws + 50 return ws print ws

OpenStudy (anonymous):

What do you have above that block?

OpenStudy (anonymous):

The code at the bottom seems a bit less convoluted. Granted, I know there will be variance in code of nearly identical programs. But is lenword or nf necessary in this block? That may be causing some sort of issue. Is it used in other parts of your code? You have a "for in range" based on lenword, as opposed to a "for in word" as shown below. def get_word_score(word, n): score = 0 for letter in word: score = score + SCRABBLE_LETTER_VALUES[letter] if len(word) == n: score = score + 50 return score

OpenStudy (anonymous):

You're calling the function with a variable cat. You want to call the function with a string "cat". Stick cat in quotation marks.

OpenStudy (anonymous):

In general, if you ever see the error message "Name ______ is not defined," it means you're using a variable that doesn't exist, and the most common errors, I would think, are forgetting to define the variable OR using a variable when you meant to use a string.

OpenStudy (anonymous):

thanks shandelman!!

OpenStudy (anonymous):

is there a way for a program to assume the input to a function is a string?

OpenStudy (anonymous):

If I understand your question, then I don't believe so. There's too much chance for ambiguity. Your function gets called with cat, and then says to itself "Okaaaay, what is cat? There are no quotation marks, so it must be a variable. Cool...so what is the variable equal to? NOTHING?! Impossible!!!!" It seems to me that if there were a programming language that could check to see if a variable is defined, and if it's not, then would assume that it's a string, a whole bunch of unintended consequences could result from that.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!