Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44671 > unrolled thread
| Started by | ryankoch38@gmail.com |
|---|---|
| First post | 2013-05-03 10:18 -0700 |
| Last post | 2013-05-07 19:19 +1000 |
| Articles | 20 on this page of 29 — 15 participants |
Back to article view | Back to comp.lang.python
First python program, syntax error in while loop ryankoch38@gmail.com - 2013-05-03 10:18 -0700
Re: First python program, syntax error in while loop John Gordon <gordon@panix.com> - 2013-05-03 17:27 +0000
Re: First python program, syntax error in while loop Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-05-03 19:26 +0200
Re: First python program, syntax error in while loop John Gordon <gordon@panix.com> - 2013-05-03 17:37 +0000
Re: First python program, syntax error in while loop MRAB <python@mrabarnett.plus.com> - 2013-05-03 18:36 +0100
Re: First python program, syntax error in while loop Zachary Ware <zachary.ware+pylist@gmail.com> - 2013-05-03 12:37 -0500
Re: First python program, syntax error in while loop ryankoch38@gmail.com - 2013-05-03 10:57 -0700
Re: First python program, syntax error in while loop John Gordon <gordon@panix.com> - 2013-05-03 18:21 +0000
Re: First python program, syntax error in while loop ryankoch38@gmail.com - 2013-05-03 11:34 -0700
Re: First python program, syntax error in while loop ryankoch38@gmail.com - 2013-05-03 12:52 -0700
Re: First python program, syntax error in while loop John Gordon <gordon@panix.com> - 2013-05-03 20:04 +0000
Re: First python program, syntax error in while loop ryankoch38@gmail.com - 2013-05-03 13:15 -0700
Re: First python program, syntax error in while loop Neil Cerutti <neilc@norwich.edu> - 2013-05-03 20:30 +0000
Re: First python program, syntax error in while loop John Gordon <gordon@panix.com> - 2013-05-03 20:38 +0000
Re: First python program, syntax error in while loop Neil Cerutti <neilc@norwich.edu> - 2013-05-06 12:06 +0000
Re: First python program, syntax error in while loop Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-05-06 13:37 +0100
Re: First python program, syntax error in while loop Neil Cerutti <neilc@norwich.edu> - 2013-05-06 13:07 +0000
Re: First python program, syntax error in while loop Roy Smith <roy@panix.com> - 2013-05-06 09:08 -0400
Re: First python program, syntax error in while loop rusi <rustompmody@gmail.com> - 2013-05-06 06:36 -0700
Re: First python program, syntax error in while loop Chris Angelico <rosuav@gmail.com> - 2013-05-06 23:37 +1000
Re: First python program, syntax error in while loop roy@panix.com (Roy Smith) - 2013-05-06 11:31 -0400
Re: First python program, syntax error in while loop Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-06 16:11 -0400
Re: First python program, syntax error in while loop Chris Angelico <rosuav@gmail.com> - 2013-05-07 07:50 +1000
Re: First python program, syntax error in while loop Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-06 20:16 -0400
Re: First python program, syntax error in while loop alex23 <wuwei23@gmail.com> - 2013-05-06 17:17 -0700
Re: First python program, syntax error in while loop Roy Smith <roy@panix.com> - 2013-05-06 20:18 -0400
Re: First python program, syntax error in while loop Dan Sommers <dan@tombstonezero.net> - 2013-05-07 05:03 +0000
Re: First python program, syntax error in while loop Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-05-07 07:10 +0100
Re: First python program, syntax error in while loop Chris Angelico <rosuav@gmail.com> - 2013-05-07 19:19 +1000
Page 1 of 2 [1] 2 Next page →
| From | ryankoch38@gmail.com |
|---|---|
| Date | 2013-05-03 10:18 -0700 |
| Subject | First python program, syntax error in while loop |
| Message-ID | <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> |
title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")
import random
tries = 0
number = random.randrange(99) + 1
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
while (guess != number):
if (guess > number):
number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
tries += 1
else if (guess < number):
number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
tries += 1
print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"
raw_input("\n\n Press any key to exit..")
## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-05-03 17:27 +0000 |
| Message-ID | <km0s19$q07$1@reader1.panix.com> |
| In reply to | #44671 |
In <24c5856e-a30a-41bd-aa4a-0e594734e1f8@googlegroups.com> ryankoch38@gmail.com writes:
> title = "Guess my number game:"
> print title.title()
> raw_input("Press any key to continue..")
> import random
> tries = 0
> number = random.randrange(99) + 1
> guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
> while (guess != number):
> if (guess > number):
> number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
> tries += 1
> else if (guess < number):
> number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
> tries += 1
> print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"
> raw_input("\n\n Press any key to exit..")
> ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P
You're missing a second closing parentheses on the line where the user
inputs their guess.
Also, you need to indent the 'if' statement underneath the while loop.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Chris “Kwpolska” Warrick <kwpolska@gmail.com> |
|---|---|
| Date | 2013-05-03 19:26 +0200 |
| Message-ID | <mailman.1272.1367602030.3114.python-list@python.org> |
| In reply to | #44671 |
On Fri, May 3, 2013 at 7:18 PM, <ryankoch38@gmail.com> wrote:
> title = "Guess my number game:"
> print title.title()
> raw_input("Press any key to continue..")
>
> import random
>
> tries = 0
> number = random.randrange(99) + 1
> guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
>
> while (guess != number):
> if (guess > number):
> number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
> tries += 1
> else if (guess < number):
> number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
> tries += 1
> print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"
>
> raw_input("\n\n Press any key to exit..")
>
> ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P
> --
> http://mail.python.org/mailman/listinfo/python-list
1. post full tracebacks.
2. The contents of your while loop must be indented, just like the
contents of the if/else if statement you have there. So, four spaces
before the 'if', 'else if' and 'print' lines; eight before 'number'
and 'tries' lines.
--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail | always bottom-post
http://asciiribbon.org | http://caliburn.nl/topposting.html
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-05-03 17:37 +0000 |
| Message-ID | <km0slk$la4$1@reader1.panix.com> |
| In reply to | #44673 |
In <mailman.1272.1367602030.3114.python-list@python.org> =?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?= <kwpolska@gmail.com> writes:
> 1. post full tracebacks.
I almost responded with the same advice, but in this case the full
traceback doesn't really tell us anything more:
File "foo.py", line 11
while guess != number:
^
SyntaxError: invalid syntax
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-05-03 18:36 +0100 |
| Message-ID | <mailman.1273.1367602615.3114.python-list@python.org> |
| In reply to | #44671 |
On 03/05/2013 18:18, ryankoch38@gmail.com wrote:
> title = "Guess my number game:"
> print title.title()
> raw_input("Press any key to continue..")
>
> import random
>
> tries = 0
> number = random.randrange(99) + 1
> guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
>
> while (guess != number):
> if (guess > number):
> number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
> tries += 1
> else if (guess < number):
> number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
> tries += 1
> print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"
>
> raw_input("\n\n Press any key to exit..")
>
> ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P
>
Indentation in important in Python.
Also, "else if" should be "elif", and you ask for a guess only once.
title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")
import random
tries = 0
number = random.randrange(99) + 1
# Ask for first guess.
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
while guess != number:
if guess > number:
number = int(raw_input("Sorry, my number is lower than that! \n
Try again:")
tries += 1
elif guess < number:
number = int(raw_input("Sorry, my number is higher than that!
\n Try again:")
tries += 1
# Ask for next guess.
guess = int(raw_input("Guess my number! Secret - It is between 1
and 100 :")
print "Congratulations, you guessed my number! \n And it only took you"
tries "tries!"
raw_input("\n\n Press any key to exit..")
[toc] | [prev] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2013-05-03 12:37 -0500 |
| Message-ID | <mailman.1274.1367602686.3114.python-list@python.org> |
| In reply to | #44671 |
On Fri, May 3, 2013 at 12:18 PM, <ryankoch38@gmail.com> wrote:
> title = "Guess my number game:"
> print title.title()
> raw_input("Press any key to continue..")
>
> import random
>
> tries = 0
> number = random.randrange(99) + 1
> guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :")
First up, there's a missing ) on each call to int(), which is causing
the syntax error you see. The error points at "while" because
"while" doesn't make sense in the call to int().
>
> while (guess != number):
> if (guess > number):
After fixing the error above, you'll have another one here: you
haven't indented the block to be executed in the while loop.
Indentation is important in Python, it delimits code blocks and makes
things more readable for people.
> number = int(raw_input("Sorry, my number is lower than that! \n Try again:")
> tries += 1
There's also another indentation error here: everything in the same
block has to be indented to the same level.
> else if (guess < number):
> number = int(raw_input("Sorry, my number is higher than that! \n Try again:")
> tries += 1
> print "Congratulations, you guessed my number! \n And it only took you" tries "tries!"
And here will be another syntax error: you have to separate your
arguments to 'print' with commas.
>
> raw_input("\n\n Press any key to exit..")
>
> ## what is wrong with this script? I'm just trying to understand while loops and ## this is not a real project :P
Once you have those errors fixed, I'll bet I can guess your number on
the second try every time. I'll let you figure out why yourself,
though ;).
[toc] | [prev] | [next] | [standalone]
| From | ryankoch38@gmail.com |
|---|---|
| Date | 2013-05-03 10:57 -0700 |
| Message-ID | <02b65e11-89c6-4639-9d93-27e1f90eed66@googlegroups.com> |
| In reply to | #44671 |
Okay, thank you very much for the timely replies, heres what i have now:
title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")
import random
number = random.randrange(99) + 1
tries = 0
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))
while (guess != number):
if (guess > number):
number = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
tries += 1
else:
number = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
tries += 1
print "good job, you won!"
raw_input("\n\n Press any key to exit..")
## it seems to stick with "higher" or "lower" after my first guess, whichever it
## is
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-05-03 18:21 +0000 |
| Message-ID | <km0v7u$op4$1@reader1.panix.com> |
| In reply to | #44678 |
In <02b65e11-89c6-4639-9d93-27e1f90eed66@googlegroups.com> ryankoch38@gmail.com writes:
> Okay, thank you very much for the timely replies, heres what i have now:
> title = "Guess my number game:"
> print title.title()
> raw_input("Press any key to continue..")
> import random
> number = random.randrange(99) + 1
> tries = 0
> guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))
> while (guess != number):
> if (guess > number):
> number = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
> tries += 1
> else:
> number = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
> tries += 1
>
> print "good job, you won!"
>
> raw_input("\n\n Press any key to exit..")
> ## it seems to stick with "higher" or "lower" after my first guess, whichever it
> ## is
When the user re-enters their guess in the while loop, you're assigning
the input to "number" instead of "guess".
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | ryankoch38@gmail.com |
|---|---|
| Date | 2013-05-03 11:34 -0700 |
| Message-ID | <324dbba9-42d5-40c7-959d-c00e914925b6@googlegroups.com> |
| In reply to | #44679 |
Oh wow I can't believed I derped that hard!!!! Thanks Lol.
[toc] | [prev] | [next] | [standalone]
| From | ryankoch38@gmail.com |
|---|---|
| Date | 2013-05-03 12:52 -0700 |
| Message-ID | <af59d0cb-5ca3-42ab-83c3-2be84da8015d@googlegroups.com> |
| In reply to | #44671 |
I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails..
title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")
import random
number = random.randrange(99) + 1
tries = 0
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))
while (guess != number):
if (guess > number):
guess = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
tries += 1
if (guess < number):
guess = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
tries += 1
if (tries > 5):
print "Sorry, you lost!"
break
print "\nCongratulations! You guessed my number in", tries, "tries"
raw_input("\n\n Press any key to exit..")
## this results in the congratulations printing after the second/third guess and ## continuing
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-05-03 20:04 +0000 |
| Message-ID | <km157f$7vo$1@reader1.panix.com> |
| In reply to | #44684 |
In <af59d0cb-5ca3-42ab-83c3-2be84da8015d@googlegroups.com> ryankoch38@gmail.com writes:
> I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails
The "Congratulations" message is inside the while loop; that's why it
always prints.
To break after five guesses, you could change the while loop to this:
while (guess != number) and (tries < 5):
And then after the while loop, put this if statement:
if (tries < 5):
print "\nCongratulations! You guessed my number in", tries, "tries"
else:
print "Sorry, you lost!"
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | ryankoch38@gmail.com |
|---|---|
| Date | 2013-05-03 13:15 -0700 |
| Message-ID | <163bd508-f225-4b4d-8173-1e070c5d5aad@googlegroups.com> |
| In reply to | #44671 |
Thank you! It's 100% functional now, here's the final project:
title = "Guess my number game:"
print title.title()
raw_input("Press any key to continue..")
import random
number = random.randrange(99) + 1
tries = 0
guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))
while (guess != number) and (tries <5):
if (guess > number):
guess = int(raw_input("Sorry, my number is lower than that! \n Try again:"))
tries += 1
if (guess < number):
guess = int(raw_input("Sorry, my number is higher than that! \n Try again:"))
tries += 1
if (tries <5):
print "\nCongratulations! You guessed my number in", tries, "tries"
else:
print "\nSorry, you took too many tries to guess my number!"
raw_input("\n\n Press any key to exit..")
## Maybe now I can work on a useful project
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-05-03 20:30 +0000 |
| Message-ID | <auil2vFijo4U1@mid.individual.net> |
| In reply to | #44686 |
On 2013-05-03, ryankoch38@gmail.com <ryankoch38@gmail.com> wrote:
> Thank you! It's 100% functional now, here's the final project:
>
> title = "Guess my number game:"
> print title.title()
> raw_input("Press any key to continue..")
>
> import random
>
> number = random.randrange(99) + 1
> tries = 0
> guess = int(raw_input("Guess my number! Secret - It is between 1 and 100 :"))
>
> while (guess != number) and (tries <5):
> [...]
>
> if (tries <5):
> print "\nCongratulations! You guessed my number in", tries, "tries"
> else:
> print "\nSorry, you took too many tries to guess my number!"
> raw_input("\n\n Press any key to exit..")
>
> ## Maybe now I can work on a useful project
Not quite yet. Players who guess correctly on the fifth try don't
get credit.
--
Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-05-03 20:38 +0000 |
| Message-ID | <km178n$5mn$1@reader1.panix.com> |
| In reply to | #44687 |
In <auil2vFijo4U1@mid.individual.net> Neil Cerutti <neilc@norwich.edu> writes:
> Not quite yet. Players who guess correctly on the fifth try don't
> get credit.
Are you sure? tries is initialized to zero and isn't incremented for the
initial guess.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-05-06 12:06 +0000 |
| Message-ID | <aupkmcF2mv6U4@mid.individual.net> |
| In reply to | #44688 |
On 2013-05-03, John Gordon <gordon@panix.com> wrote: > In <auil2vFijo4U1@mid.individual.net> Neil Cerutti <neilc@norwich.edu> writes: > >> Not quite yet. Players who guess correctly on the fifth try don't >> get credit. > > Are you sure? tries is initialized to zero and isn't > incremented for the initial guess. while (number != guess) and (tries < 5): Is the condition that concludes the game. After the game, you are told you lost if tries is not less than five, regardless of if number == guess. -- Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-05-06 13:37 +0100 |
| Message-ID | <mailman.1360.1367843880.3114.python-list@python.org> |
| In reply to | #44825 |
On 06/05/2013 13:06, Neil Cerutti wrote: > On 2013-05-03, John Gordon <gordon@panix.com> wrote: >> In <auil2vFijo4U1@mid.individual.net> Neil Cerutti <neilc@norwich.edu> writes: >> >>> Not quite yet. Players who guess correctly on the fifth try don't >>> get credit. >> >> Are you sure? tries is initialized to zero and isn't >> incremented for the initial guess. > > while (number != guess) and (tries < 5): > > Is the condition that concludes the game. > > After the game, you are told you lost if tries is not less than > five, regardless of if number == guess. > One of these days I'll work out why some people insist on using superfluous parentheses in Python code. Could it be that they enjoy exercising their fingers by reaching for the shift key in conjunction with the 9 or 0 key? -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2013-05-06 13:07 +0000 |
| Message-ID | <aupo88F2mv6U5@mid.individual.net> |
| In reply to | #44826 |
On 2013-05-06, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > On 06/05/2013 13:06, Neil Cerutti wrote: >> On 2013-05-03, John Gordon <gordon@panix.com> wrote: >>> In <auil2vFijo4U1@mid.individual.net> Neil Cerutti <neilc@norwich.edu> writes: >>> >>>> Not quite yet. Players who guess correctly on the fifth try don't >>>> get credit. >>> >>> Are you sure? tries is initialized to zero and isn't >>> incremented for the initial guess. >> >> while (number != guess) and (tries < 5): >> >> Is the condition that concludes the game. >> >> After the game, you are told you lost if tries is not less than >> five, regardless of if number == guess. > > One of these days I'll work out why some people insist on using > superfluous parentheses in Python code. Could it be that they > enjoy exercising their fingers by reaching for the shift key in > conjunction with the 9 or 0 key? Superflous parenthesis are sometimes a nice aid to comprehension. I don't know about the above case, though. I don't think it hurts anything, but it doesn't add much. -- Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-05-06 09:08 -0400 |
| Message-ID | <roy-76F87B.09084406052013@news.panix.com> |
| In reply to | #44826 |
In article <mailman.1360.1367843880.3114.python-list@python.org>, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > > while (number != guess) and (tries < 5): > > One of these days I'll work out why some people insist on using > superfluous parentheses in Python code. Could it be that they enjoy > exercising their fingers by reaching for the shift key in conjunction > with the 9 or 0 key? There's lots of reasons. Some valid, some not. Lets dispense with the invalid reason first. They've come from C/C++/Java/whatever and are used to typing parens around the conditions for if/for/while statements. To them, I say, "Stop trying to write FORTRAN code in languages that aren't FORTRAN". In this case, however, I have no problem with the extra parens. Look at these two statements: >> while (number != guess) and (tries < 5): >> while number != guess and tries < 5: They have the same meaning. To correctly interpret the second one, you need to know that != and < bind tighter than "and". One could say that you should know that, and maybe you would be right. On the other hand, I've long since given up trying to remember operator precedence in various languages. If I ever have even the slightest doubt, I just go ahead and put in the extra parens. It takes another few ms to type, and it removes all ambiguity (for both me, and every future person who has to read my code). And, every once in a while, it keeps me from writing a subtle and hard-to-find bug because the precedence rules I was sure I had remembered correctly turned out to be wrong for the language I happened to be typing that day. BTW, in C, I used to write: return (foo) for years until somebody pointed out to me that return foo works. I just assumed that if I had to write: if (foo) while (foo) for (foo; bar; baz) then return (foo) made sense too.
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-05-06 06:36 -0700 |
| Message-ID | <bfd67f87-4ef9-4159-9a41-0fbcdd284eb7@n5g2000pbg.googlegroups.com> |
| In reply to | #44828 |
On May 6, 6:08 pm, Roy Smith <r...@panix.com> wrote: > BTW, in C, I used to write: > > return (foo) > > for years until somebody pointed out to me that > > return foo > > works. I just assumed that if I had to write: > > if (foo) > while (foo) > for (foo; bar; baz) > > then > > return (foo) > > made sense too. I guess its because K&R always show their examples with return (expr); and so we all assume its the propah way
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-05-06 23:37 +1000 |
| Message-ID | <mailman.1361.1367847484.3114.python-list@python.org> |
| In reply to | #44828 |
On Mon, May 6, 2013 at 11:08 PM, Roy Smith <roy@panix.com> wrote: > On the other hand, I've long since given up trying to remember operator > precedence in various languages. If I ever have even the slightest > doubt, I just go ahead and put in the extra parens. If I ever have even the slightest doubt, I just go ahead and type "<language> operator precedence" into a web search and check it :) Aside from utter insanity like PHP's ternary operator being wrongly associative, that covers pretty much everything. ChrisA
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web