FAQ: Functions - Review: Functions

This community-built FAQ covers the “Review: Functions” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python

FAQs on the exercise Review: Functions

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

In exercise 17/19, I do not understand how to make this “def speak(message):” example code run without errors.

I am able to make the Instructions shutdown() code operate ok but the example code looks weird somehow ???

Code work in Python 3.7.1, but in exercise 17/19 not work. Please, help.

2 Likes

If I’m not mistaken, CodeCademy has Python 2, not 3.7.1. Most sites has a 3 at the most.

I hope this helps =)

1 Like

It looks to be working in the exercise environment. Perhaps it is not being accepted by the lesson checker? If not, then remove the print statements and only print the return value at the caller.

2 Likes

I could not understand the instructions. It looks like I can run the code without using “return”

Below is the code (indentation not reflecting here) and it runs what is required. I am trying to understand the difference between print and return.

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

shut_down(“yes”)

When I tried this code with return and it looks like it is going as a indefinite loop. Throwing an error - “max recursion depth exceeded.”

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

shut_down(“yes”)

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

hi. why cant I use =
why must it be ==

hi. why cant I use =
why must it be ==

= is for assignment, only. == is for comparing, as in testing for equality.

5 Likes

Hey folks,

I seem to be misunderstanding boolean operators. I’ve made an attempt at adding a little extra to exercise 17/19, but have run into a problem.

userInput = raw_input("Shutdown?(yes/no):")

def shut_down(s):
  #shutdown approved if user enters "yes" or "y"
  if s.lower() == ("yes" or "y"):
    return "Shutting down"
  #shutdown prevented if user enters "yes" or "y"
  elif s.lower() == ("no" or "n"):
    return "Shutdown aborted"
  else:
    return "Inappropriate Input"
  
print shut_down(userInput)

When I try to use “y” or “n” as an input the program runs the else condition. Am I misusing the or operator? Is it a syntax error?

It will work if I use the or operator for 2 expressions instead of 2 arguments:

userInput = raw_input("Shutdown?(yes/no):")

def shut_down(s):
  #lowercase return of userInput
  x = s.lower()
  #shutdown approved if user enters "yes" or "y"
  if x == "yes" or x == "y":
    return "Shutting down"
  #shutdown prevented if user enters "yes" or "y"
  elif x == "no" or x == "n":
    return "Shutdown aborted"
  else:
    return "Inappropriate Input"
  
print shut_down(userInput)

I don’t like this code, it feels inefficient.

Thank you,

Maxwell

1 Like

yes, thank you @mtf, that did the trick for me. Indeed code is correct but you need to use exactly what the instructions tells you.

1 Like

Math has order operations, so has programming when it comes to conditions.

Lets do this line:

if s.lower() == ("yes" or "y"):

the parentheses obviously go first, which results in true (strings are considered true), lets say the user entered Y then we get:

if "y" == true

(i also did the lowercase conversion), this condition is false, a string does not equal boolean.

1 Like

I think it’s because after return you are calling for the function shut_down instead of returning the string “Shutting down”. This will cause an infinite loop.

I am stuck on this one too. Here is the code that I am trying to use:
def shut_down(s):

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

Based on the instructions, as far as I can tell, this is all that I need to do, however I keep throwing errors. As I currently have it setup above, I get this error: “Your shut_down function threw the following error: maximum recursion depth exceeded”. Without the extra indentation, and formatting my code to look like the example, I get this error: “Your code looks a bit off. Feel free to peek back at earlier exercises if you need a refresher! Your code threw the following error: expected an indented block…”.

I don’t understand why my code is not working.

4 Likes

Hi @dapeej:

The function header should look like this:

def shut_down(s):

After the function header, begin the code within the function as follows:

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

Avoid calling the shut_down function within the function, in other words, do not do this:

if shut_down(s) == "yes":

Check the indentation of each line.

3 Likes

Thank you for your assistance with this this. I checked what you suggested, and it worked!

2 Likes

QQ%E6%88%AA%E5%9B%BE20190329000322
Why it report this error?

1 Like

the terminal tells you where there is a syntax error. return keyword can only be used within a function. Which you don’t do

the bigger problem is the program you are asked to make follows a very different design then the example provided in the lesson. You can’t just go about copying code and changing a few things hoping it works

2 Likes

Why am I getting this error, and what is the right way to solve this? Feedback will be appreciated

Regards

1 Like

in python2, input() evaluates the input as code. use raw_input() instead

2 Likes