FAQ: Functions - Review: Functions

It worked! Thanks a lot! I recall trying raw_input before but it threw an error that time. It worked now though :slight_smile:

Python has two major versions: python2 and python3. Python3 is the newer and should be used, but is not backward compatible with python2. raw_input vs input is a good example of this.

python2 is also near its End of Life (EOL).

1 Like

What am I doing wrong with line 5? I tried moving around the “return”

The example is just that, an example. Your program should have a very different structure. return can only be used in the body of a function.

1 Like

S is not defined, yet i’m missing how the variables were defined in the examples. What should I be looking at?

Currently you have an identity function…

def shut_down(s):
    return s

Then you have some code that includes the variable s which was defined not in global scope, but in the function. It will surely raise an error.

How do we get that code into the function, before the return statement, and also ensure the return value is correct for our purposes?

1 Like

Hi there,
Just playing around with this exercise:
I’ve got two codes here - one works but the other doesn’t…It would be great to get some help understanding this! Thank you!

The code (indents don’t seem to show up):

def shut_down(s):
if s == “yes” or “Yes” or “Y” or “y”:
print “Shutting down”
elif s == “no” or “No” or “n” or “N”:
print “Shutdown aborted”
else:
print “Sorry”

shut_down(“no”)

This prints “Shutting down”, but the argument I inputted was “no”. Why is this!?

This alternative version of the code works perfectly:
def shut_down(s):
s = s.lower()
if s == (“yes”):
print “Shutting down”
elif s == “no”:
print “Shutdown aborted”
else:
print “Sorry”

shut_down(“No”)

I should add that, with the first example, if I replace every or with and then the code does work. But this makes no sense to me either! In an earlier lesson, and and or were defined as:
" * and , which checks if both the statements are True ;

  • or , which checks if at least one of the statements is True ;"

So based on this above definition, or seems like the more logical choice than and surely?..

Thank you for your help!
Kind regards,
Alex

Ah i half just figured it out!!:

def shut_down(s):
if s == “yes” or s == “Yes” or s == “Y” or s == “y”:
print “Shutting down”
elif s == “no” or s == “No” or s == “N” or s == “n”:
print “Shutdown aborted”
else:
print “Sorry”

This now works perfectly.

def shut_down(s):
if s == “yes” and s == “Yes” and s == “Y” and s == “y”:
print “Shutting down”
elif s == “no” and s == “No” and s == “N” and s == “n”:
print “Shutdown aborted”
else:
print “Sorry”

This just prints “Sorry” whatever you write. So that explains I was correct to want to use or, rather than and, I just hadn’t written the correctly!

But I still don’t understand why and works when I wrote it incorrectly!?

Using and means all of the conditions have to be true. Which can’t be. How can one string equal multiple different strings?