On Wed, Feb 2, 2011 at 7:23 PM, John Parker <park...@gmail.com> wrote:
> All,
>
> I have written this test code
>
> if (card == "Hearts" or card == "Diamonds"):
>     print "That card is Red"
>
> elif (card == "Spades" or card == "Clubs"):
>     print "That card is Black"

Note that the parentheses are completely unnecessary and not idiomatic style.
    if card == "Hearts" or card == "Diamonds":
is the normal way of writing compound conditions.

> It seems to work but prior to this code, it looked like this
>
> if (card == "Hearts" or card == "Diamonds"):
>     print "That card is Red"
>
> elif (card == "Spades" or card == "Clubs")
>     print "That card is Black"
>
> Which didn't work.  Can someone set me straight because in class
>
> The teacher gave an example like this which worked
>  if (favcolor == "Red"):
>    print "Roses are Red"
>
>  elif (favcolor == "Blue"
>    print "Violets are Blue"

I can assure you that such code does not work. Either the example was
incorrect or there is some other key difference you accidentally
omitted. The colons are absolutely required.

> So, why did I have to add a second : after my code for spades or clubs?

All Python control structures require a colon.
The general form is:
<control keyword> <stuff>:
    <body>

In the specific case of if-elif-else, we have:
if condition:
    do_something
elif other_condition:
    do_something_else
else:
    do_a_third_thing

All the colons are always mandatory.

Also, your question isn't Mac-specific, so for future reference, it
would have been better posed to the more general and widely-read
python-list (http://mail.python.org/mailman/listinfo/python-list ).

Cheers,
Chris
--
http://blog.rebertia.com
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG

Reply via email to