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!
Latest Questions
unknownnnnnn: Static at 2 A.M. My mind doesnu2019t knock. It rearranges the furniture at 2 a.m., asks me to notice every creak. I lie still like that might help, like silence is a language my thoughts forgot. They line up with receipts, proof of moments I replayed too many times to pretend they were accidents. Iu2019m fluent in overthinking itu2019s the only subject I never skipped. I can turn one sentence into a courtroom drama, cross-examine my tone, convict myself without witnesses. People call me u201cstrongu201d because I donu2019t spill. They donu2019t see the cup shaking in my hands, how much effort it takes to keep the surface calm. Confidence comes in phases. Some days it fits like skin. Some days itu2019s a costume I forget Iu2019m wearing until it starts to pinch. I laugh on cue. I answer u201cfineu201d with convincing timing. Iu2019ve learned where to pause, how long eye contact should last, how not to sound like a question when Iu2019m one. The past isnu2019t loud. It doesnu2019t need to be. It just clears its throat at the wrong moments, reminds me what I already survived and what might try again. But hereu2019s the part I donu2019t downplay I stay. Even when my thoughts argue in circles, even when doubt files appeals. I choose presence over perfection. Breath over escape. I donu2019t win every round, but I donu2019t forfeit myself either. I am not the static. I am the one listening, deciding what deserves a response and what can fade without taking my name with it.
22 hours ago 2 Replies 0 Medals
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!