Example does not match activity


#1

In the “Let’s Review” example below, we define the function “speak” with the argument “message.” We then immediately return “message,” and it never comes up again in the script. The script then uses variables “happy” and “sad” that were never defined, rather than using “return” like it does in the answer key beneath “if,” “elif,” and “else.” The provided answer is easy enough to understand, but the initial “let’s review” example makes no sense, where the function “speak” is called rather than “return.”

Let’s review (script in question)

def speak(message):
return message

if happy():
speak(“I’m happy!”)
elif sad():
speak(“I’m sad.”)
else:
speak(“I don’t know what I’m feeling.”)

Answer key:

def shut_down(s):
if s == “yes”:
return "Shutting down"
elif s == “no”:
return "Shutdown aborted"
else:
return “Sorry”


#2

I don’t see a problem? If the function would follow the design pattern of the solution, you never learn to program and design a solution.


#3

That’s understandable. But it doesn’t seem to be following any logical design pattern at all. Why is “message” being returned before the conditional statement even occurs? Why is “speak” being used in place of “return”? How are “happy” and “sad” being used in if/elif statements if they haven’t been defined? Is it asking for user input and expecting “happy” and “sad” as responses?


#4

Again, the example code above is just there for your reference!

it seems to be a reference to remind you of syntax


#5

Thanks for the feedback. If learning to code is about noticing and repeating patterns, then examples like that are just misleading. It’s not even a script that could be run! Wouldn’t “message” need to == “happy” or “sad”? Not sure what syntax I’m being reminded of there if it doesn’t make any sense. I could say:

def blue(yellow):
return yellow

if dog():
blue(“I’m a dog!”)
elif cat():
blue(“I’m a cat.”)
else:
blue(“What was yellow again?”)

Anyway, thanks again.


#6

personally, i would change it to:

def speak(message):
    return message

feeling = raw_input("how are we feeling today? ")
if feeling == "happy":
    speak(“I’m happy!”)
elif feeling == "sad":
    speak(“I’m sad.”)
else:
    speak(“I don’t know what I’m feeling.”)

#7

That’s much more helpful! Thank you!


#8

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