newbie: Syntax error

Matt matthew_shomphe at countrywide.com
Fri Feb 4 13:56:06 EST 2005


Chad Everett wrote:
> Hi Everyone,
>
> I am new to Python and programming in general.  I bought the book
"Python
> Programming for the Absolute Beginner" by michael Dawson.
>
> I have been working through it but am having trouble.
> I am trying to make a coin flip program and keep geting a Synax Error
> "invalid syntax".
>
> If anyone has a moment could you please look at it and tell me what I
am
> doing wrong.
>
> thanks for your time and patience.
>
> Chad
>
> # Coin Flip Program
> # This program flips a coin 100 times and tells the number of heads
and
> tails.
> # Chad Everett 2/3/2005
>
>
> print "\t\t********Coin Flip Game*********\n"
> import random
>
> # heads = 1
> # tails = 2
>
> tries = random.randrange(2) + 1
> count = 1
>
> while count != 100:
>     if tries == 1:
>         heads = 1
>         count += 1
>
>     else tries == 2:  # I AM GETTING THE SYNTAX ERROR HERE
>         tails = 1
>         count += 1
>
> print "heads: " + heads
> print "tails: " + tails
>
> raw_input("Press enter to quit")

Chad,

The problem is that you have the statement "tries == 2" after "else".
The proper syntax is:
>     elif tries == 2:  # note "elif"
>         tails = 1
>         count += 1

Here's an example of if/elif/else statements in Python:
http://www.network-theory.co.uk/docs/pytut/tut_21.html

Enjoy learning!




More information about the Python-list mailing list