| Author |
Message |
John Machin *nix forums Guru
Joined: 19 Feb 2005
Posts: 608
|
Posted: Fri Feb 04, 2005 7:25 pm Post subject:
Re: newbie: Syntax error
|
|
|
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.
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
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 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
|
Posted: Fri Feb 04, 2005 6:00 pm Post subject:
Re: newbie: Syntax error
|
|
|
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
|
Posted: Fri Feb 04, 2005 5:57 pm Post subject:
Re: newbie: Syntax error
|
|
|
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
|
Posted: Fri Feb 04, 2005 5:56 pm Post subject:
Re: newbie: Syntax error
|
|
|
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
|
Posted: Fri Feb 04, 2005 5:54 pm Post subject:
Re: newbie: Syntax error
|
|
|
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
|
Posted: Fri Feb 04, 2005 5:49 pm Post subject:
newbie: Syntax error
|
|
|
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 |
|
 |
|