"Paul W Peterson" <[EMAIL PROTECTED]> wrote
> "Guess my number" (code pasted below). His challenge is to modify the
> code so that there are only a limited number of attempts before the
> program exits with a chastising message.
>
> Try as I may, I cannot seem to get the syntax correct for nested while
> loops.
I wouldn't use a nested loop for that but an if/break at the top of
the existing loop.
But nested loops are easy, its just an extra indentation level:
>>> j = 0
>>> while j < 5:
... n = 0
... while n < 10:
... print n,
... n += 1
... j += 1
...
> using nested while statements, or suggest a better use of the ifs?
Using 'if's I'd do:
# guessing loop
while (guess != the_number):
if tries >= TRY_LIMIT:
print "Too many tries"
break
if (guess > the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
> Also, while asking for help from this forum: for short pieces of code
> such as these, is it appropriate to enter them as inline text
Yes, if it gets above a screenful(50 lines?) then an
attachment (or a url) is usually better.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor