Trouble with Exercise: LearnPython-Functions-17:Review:Functions


#1

I’m stuck on exercise 17 with Functions with Python. The link is:
https://www.codecademy.com/en/courses/learn-python/lessons/functions/exercises/review-functions?action=lesson_resume&link_content_target=interstitial_undefined

Please see the following:

Code Error

def shut_down(s):
return s

if Yes():
print "True"
elif No():
print "False"
else:
print “sorry”

Error
name ‘Yes’ is not defined

Code

def shut_down(s):
return s

if Yes():
print "True"
return s
elif No():
print "False"
return s
else:
print "Sorry"
return s

Error:

‘return’ is an outside function

Code:

def shut_down(s):
return s

if Yes():
print "True"

return s
elif No():
print "False"
return s
else:
print “Sorry”

Errors: unexpected indent, python line 4,
then unexpected indent, python line 7

Code:

def shut_down(s):
return s

if Yes():
print "True"
return s
elif No():
print "False"
return s
else:
print "Sorry"
return s

Error:
File “python”, line 7 elif No():
SyntaxError: invalid syntax

I wrote code according to the instructions and example code, then I modified in series according to the errors given.


#2

Every one of those return lines can be removed, especially the first one. The function does not even run the code that follows. Anything after return is unreachable.

Is there a function somewhere called Yes? Or one called No? Those lines are supposed to be conditions.

Let’s review the instructions…

First, def a function, shut_down, that takes one argument s. Don’t forget the parentheses or the colon!

Then, if the shut_down function receives an s equal to “yes”, it should return “Shutting down”

Alternatively, elif s is equal to “no”, then the function should return “Shutdown aborted”.

Finally, if shut_down gets anything other than those inputs, the function should return “Sorry”

We can see that your first line is correct, but where that return line came from is anyone’s guess. Do you not understand the instructions? Was this code copied from somewhere? (First rule of coding is to never use code you do not understand.)

So what is the first condition? Are we directed to print anything in the function? I don’t see any such instruction. The condition should return a value, as given, and not print anything. Never write code that is not asked for since the SCT will not be expecting it.

if s == 'yes':
    return "Shutting down"

Now see if you can finish this, following exactly along with the instructions.


#3

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