niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
newbie: Syntax error
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
Author Message
John Machin
*nix forums Guru


Joined: 19 Feb 2005
Posts: 608

PostPosted: Fri Feb 04, 2005 7:25 pm    Post subject: Re: newbie: Syntax error Reply with quote

Chad Everett wrote:
Quote:
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.

Oh, no, it doesn't! Once you fix the few things that stop it from
running at all, it will flip the coin ONCE and tell you the one
then-fixed answer *99* times.

Quote:
# Chad Everett 2/3/2005


print "\t\t********Coin Flip Game*********\n"
import random

# heads = 1
# tails = 2

tries = random.randrange(2) + 1

Better move the above line _after_ the "while" statement.

Quote:
count = 1

Try 0; think about it this way: you are counting the number of times
something has happened, and you want to stop after 100 somethings have
happened. At this stage you ain't hatched no chickens :)

Quote:

You also need to initialise the 'heads' and 'tails' counters.


Quote:
while count != 100:

Defensive programming hint #1: while count <= 100, if starting at 1; <
100 if starting from 0

Tip: Python takes the drudgery out of programming. Less typing, less
scope for error. To do something 100 times:

!for count in range(100):
! do_something()

Quote:
if tries == 1:
heads = 1

I think you meant:
!heads += 1

Quote:
count += 1
Do this in only one place; it doesn't/shouldn't depend on the coin toss

result.

Quote:

else tries == 2: # I AM GETTING THE SYNTAX ERROR HERE
tails = 1

and
!tails += 1

Quote:
count += 1


Others have answered your syntax error question; I'll just round off
with Defensive Programming hint #2:

Get into this habit early:

!if tries == 1:
! blah_blah()
!elif tries == 2:
! yadda_yadda()
!else:
! raise Exception, "Can't happen Smile tries has unexpected value
(%r)" % tries

Quote:
print "heads: " + heads

Ugh; did you get that out of the book?

Try:
!print "heads:", heads

Quote:
print "tails: " + tails

raw_input("Press enter to quit")

HTH,
John
Back to top
Bill Mill
*nix forums Guru Wannabe


Joined: 01 Mar 2005
Posts: 117

PostPosted: Fri Feb 04, 2005 6:00 pm    Post subject: Re: newbie: Syntax error Reply with quote

Chad,

try "elif tries == 2" or just "else:". You are not allowed to put an
expression after an else statement.

I recommend you read the python tutorial at
http://docs.python.org/tut/tut.html .

Peace
Bill Mill
bill.mill at gmail.com


On Fri, 4 Feb 2005 12:49:51 -0600, Chad Everett <everettcc@hotmail.com> wrote:
Quote:
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")

--
http://mail.python.org/mailman/listinfo/python-list
Back to top
F. Petitjean
*nix forums beginner


Joined: 03 Mar 2005
Posts: 46

PostPosted: Fri Feb 04, 2005 5:57 pm    Post subject: Re: newbie: Syntax error Reply with quote

Le Fri, 4 Feb 2005 12:49:51 -0600, Chad Everett a écrit :
Quote:
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 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
elif tries == 2: # should be better
tails = 1
count += 1

print "heads: " + heads
print "tails: " + tails

raw_input("Press enter to quit")

Back to top
Matt
*nix forums Guru Wannabe


Joined: 05 Mar 2005
Posts: 166

PostPosted: Fri Feb 04, 2005 5:56 pm    Post subject: Re: newbie: Syntax error Reply with quote

Chad Everett wrote:
Quote:
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:
Quote:
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!
Back to top
Kartic
*nix forums addict


Joined: 22 Feb 2005
Posts: 62

PostPosted: Fri Feb 04, 2005 5:54 pm    Post subject: Re: newbie: Syntax error Reply with quote

Chad,

The usage is like this:
- if <cond1>:
- <block 1>
- elif <cond2>:
- <block 2>
- else:
- <block 3>

So, your code should be:
if tries == 1:
.....
elif tries == 2:
.....

(You have 'else' followed by a condition...hence a syntax error. else
is what it means - "if no other condition is satisfied". You should use
'elif' for subsequent conditions after 'if'.)

Thanks,
-Kartic
Back to top
Chad Everett
*nix forums beginner


Joined: 04 Feb 2005
Posts: 10

PostPosted: Fri Feb 04, 2005 5:49 pm    Post subject: newbie: Syntax error Reply with quote

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")
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [6 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 11:30 pm | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Postfix + MySQL error: very strange variable %s iWarior Postfix 0 Mon Aug 25, 2008 2:01 pm
No new posts ** Postfix error on console every minute or so ** ?? drywash Postfix 0 Fri Jul 04, 2008 8:49 pm
No new posts Postfix error bounce diwash Postfix 0 Fri Mar 28, 2008 3:37 am
No new posts I am getting following error in Aix 5.3 rockcharles1 AIX 0 Tue Aug 28, 2007 11:06 pm
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm

Find a Credit Card | Credit Card information | Sudoku Software | Credit Report | Credit Counseling
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2853s ][ Queries: 20 (0.1847s) ][ GZIP on - Debug on ]