0

On Code Academy there is this course where in the example they show

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.")

The above example will NOT be related to the rest of the code I show. That was just an example for the if statement. Now I was under the impression that when ever writing an if statement it had to end in an ():like the above example.

However when doing the assignments this does not work:

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

However this works:

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

My question is how come the () is not needed next to the "yes" and "no" but :is still needed. I thought whenever writing an if statement it will automatically have to end with ():. In that very first example, that's how it is shown. Do you understand my confusion.

Koda James
  • 53
  • 6
  • 2
    No, that's not the case. See [what the official tutorial says about the `if` statement](https://docs.python.org/3/tutorial/controlflow.html#if-statements). – Dan Getz Jul 29 '15 at 20:05
  • 4
    It looks like you have a fundamental misunderstanding about [functions](https://docs.python.org/3.4/tutorial/controlflow.html#defining-functions) and [`if` statements](https://docs.python.org/3.4/tutorial/controlflow.html#if-statements). – TigerhawkT3 Jul 29 '15 at 20:05
  • 1
    Happy and sad are probably [functions](http://stackoverflow.com/a/19131217/5031339) – NightShadeQueen Jul 29 '15 at 20:06
  • 1
    @chrisaycock I think you're thinking of a different kind of assignment than the OP – StephenTG Jul 29 '15 at 20:07

4 Answers4

5

In the example given, happy() and sad() are functions, and as such require parentheses. The if itself does not need parentheses at the end (and it shouldn't have them)

StephenTG
  • 2,579
  • 6
  • 26
  • 36
4

No, if has nothing to do with ()

happy is a function. happy() is a call to that function. So, if happy(): tests if the happy function returns true when called.

In other words, if happy(): speak("I'm happy!") is equivalent to

result_of_happy = happy()
if result_of_happy:
    speak("I'm happy!")
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
2

As has been mentioned happy() / sad() are functions so they require (). In example two of your question you are comparing your value to the string "yes" because it is a string it does not require ().

Within an if statement you can use parentheses to make the code more readable and ensure certain operations are evaluated before others.

if (1+1)*2 == 4:
    print 'here'
else:
    print 'there'

Differs from:

if 1+1*2 == 4:
    print 'here'
else:
    print 'there'
user204088
  • 1,805
  • 3
  • 17
  • 21
0

Because string objects are not callable so what are you expecting then:

Then use lambda not that efficient tho:

def shut_down(s):
    if (lambda: s == "yes")():
        return "Shutting down"
    elif (lambda: s == "no")():
        return "Shutdown aborted"
    else:
        return "Sorry"
U13-Forward
  • 69,221
  • 14
  • 89
  • 114