jharris1993
Posts: 32
Joined: Sat Oct 05, 2013 10:26 pm

Continue *after* executing statements within a code block?

Fri Oct 18, 2019 2:24 am

Greetings!

Yes, I have already tried consulting all the usual fountains of wisdom before posting...

I am still working on my Craps game, and I have run into an interesting situation.

I understand that the "continue" statement is normally used like this:

Code: Select all

while [something is true]
     code
     code
     if [something happens]:
          do this
          do that
          do something else
     else:
          continue
     more code
     etc.
     etc.
What I need to do is more like this:

Code: Select all

while [something is true]
code
code
if [something happens]:
     do this
     do that
     do something else
     continue  # after doing the few things above, *then* skip back to the loop
                       # ignoring the rest of the body of the loop
next part of conditional block
etc.
In other words, I want something specific to happen if the condition is met, before breaking out and skipping back to the beginning of the loop.

To be more specific, in Craps if the shooter - while trying to make his 'point' (the dice throw that will allow him to win), rolls certain numbers, they "don't count", (that is 12, 11, or 3 in my game's case), and he is automatically allowed to continue.

In my case, I am allowing the shooter a limited number of throws - let's say 10. If the dice are thrown ten times and he doesn't make his point, he looses. However, if he throws one of those special numbers, it should not count against him. Ergo the "if [one of these numbers] - skip back to the beginning of the loop again, without incrementing the dice--throw counter at the end of the loop.

For testing purposes - to help find a different bug - I want the fact that one of these special numbers was thrown to print something, then "continue" back at the beginning of the loop ignoring the rest of the loop's body.

How do I do this?

=========================================

Update:
As far as I can tell, the "continue" statement has two restrictions:
1. It must be on a line of it's own (properly indented, of course)
2. It must be the only statement after the conditional that triggers it. (Dumb, but I didn't write the language. . .)

I came up with a workaround:
In the place of the continue in the previous code block, I inserted a dummy conditional, something like "if True" followed by "continue".

Viz.:

Code: Select all

while [something is true]
code
code
if [something happens]:
     do this
     do that
     do something else
=>   if True:
=>     continue
next part of conditional block
etc.
IMHO, this is a butt-ugly hack. Surely there must be a better way, 'eh?
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

User avatar
rpdom
Posts: 15426
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Continue *after* executing statements within a code block?

Fri Oct 18, 2019 5:48 am

If I get you right, I think you are trying to overcomplicate the code. Why not just use a set of if...elif...elif...else tests?

Code: Select all

while [something is true]:
    code
    code
    if [something happens]:
        do this
        do this
        do something else
    elif [somthing else happens]:
        do this
        do this
        do another thing
    elif [check for another thing]:
        do that
        do things
    else:
        do loads of other stuff
        all the other code
        everything here will be run
        only if the first two conditions
        are not met
    code here will be run regardless of the conditions
loop has ended

User avatar
PeterO
Posts: 5084
Joined: Sun Jul 22, 2012 4:14 pm

Re: Continue *after* executing statements within a code block?

Fri Oct 18, 2019 7:23 am

jharris1993 wrote:
Fri Oct 18, 2019 2:24 am
Update:
As far as I can tell, the "continue" statement has two restrictions:
1. It must be on a line of it's own (properly indented, of course)
2. It must be the only statement after the conditional that triggers it. (Dumb, but I didn't write the language. . .)
https://www.tutorialspoint.com/python/p ... tement.htm

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PiGraham
Posts: 3666
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Continue *after* executing statements within a code block?

Fri Oct 18, 2019 11:54 am

jharris1993 wrote:
Fri Oct 18, 2019 2:24 am

Update:
As far as I can tell, the "continue" statement has two restrictions:
1. It must be on a line of it's own (properly indented, of course)
2. It must be the only statement after the conditional that triggers it. (Dumb, but I didn't write the language. . .)
I don't think that's right. You can have other statements in the conditional before a continue or break.

Python 3

Code: Select all

for i in range(1,10):
    if(i>=5 and i<7):
        print("======")
        continue
    print(i)
    
    
    Prints:
    
  1
  2
  3
  4
======
======
7
8
9
Last edited by PiGraham on Fri Oct 18, 2019 12:09 pm, edited 1 time in total.

User avatar
PeterO
Posts: 5084
Joined: Sun Jul 22, 2012 4:14 pm

Re: Continue *after* executing statements within a code block?

Fri Oct 18, 2019 12:07 pm

PiGraham wrote:
Fri Oct 18, 2019 11:54 am
Python 3

Code: Select all

for i in range(1,10):
    if(i>=5 and i<7):
        print("======")
        continue
    print(i)
    
    
    Prints:
    
  1
  2
  3
  4
======
======
8
9
There is somethign seriously wrong with your Python3 installation !

Code: Select all

Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(1,10):
...     if(i>=5 and i<7):
...         print("======")
...         continue
...     print(i)
... 
1
2
3
4
======
======
7
8
9
;)
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PiGraham
Posts: 3666
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Continue *after* executing statements within a code block?

Fri Oct 18, 2019 12:10 pm

PeterO wrote:
Fri Oct 18, 2019 12:07 pm

There is somethign seriously wrong with your Python3 installation !

;)
PeterO
Ha ha! Yes indeed. It's the dodgy programmer with error prone copy-paste!

User avatar
PeterO
Posts: 5084
Joined: Sun Jul 22, 2012 4:14 pm

Re: Continue *after* executing statements within a code block?

Fri Oct 18, 2019 12:13 pm

Dupe caused by Forum Servers having a lie down for a few minutes :-)
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jharris1993
Posts: 32
Joined: Sat Oct 05, 2013 10:26 pm

Re: Continue *after* executing statements within a code block?

Fri Oct 18, 2019 11:21 pm

rpdom wrote:
Fri Oct 18, 2019 5:48 am
If I get you right, I think you are trying to overcomplicate the code. Why not just use a set of if...elif...elif...else tests?

Code: Select all

while [something is true]:
    code
    code
    if [something happens]:
        do this
        do this
        do something else
    elif [something else happens]:
        do this
        do this
        do another thing
    elif [check for another thing]:
        do that
        do things
    else:
        do loads of other stuff
        all the other code
        everything here will be run
        only if the first two conditions
        are not met
    code here will be run regardless of the conditions
loop has ended
In my case "the rest of the code" does things that I don't want done. If a particular and specific condition is met, (the shooter rolls a "natural" (i.e. 12, 11, or 3), the rest of the loop should be skipped and the number of dice throws should not increment - since my game gives the shooter only so many dice rolls.
If you look at the examples shown in your link, along with every other example on Google, DogPile, DuckDuckGo, ad nauseum, you will notice that all of them, every single one, show the "continue" or "break" coming directly after the line that makes the test. In my case, whether within this code, or within simple "test" scripts, there was no, repeat ZERO, way that I could get either "break" or "continue" to work if there were any active statements between the conditional test and the break or continue. The only way I could get it to work after a block of statements was to include a "dummy" test followed immediately by the "continue".

Important question:
What version of Python are you using, and on what system? Mine is 3.7.3 on a Raspberry Pi 4 with what I believe is the latest version of Raspbian, (Sep. 2019), fully up to date.

One thing I have noticed, both in my exhaustive researches and while trying things here on my Pi, is that versions matter. Even across point-releases in the 3.n branch. I've read from a number of different sources on the 'Net that the 3.7 point-release changed much in Python from other, earlier, release versions. So much so that I am surprised that they didn't just bump a major revision and call it 4.n.

3.7.5 makes major revisions to Python compared to 3.7.3, and the upcoming 3.8 release does even more. I am inclined to agree with Dougie's assessment of the current state of Python development as ". . .a steaming pile of [censored]!" :?
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

User avatar
rpdom
Posts: 15426
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Continue *after* executing statements within a code block?

Sat Oct 19, 2019 7:00 am

jharris1993 wrote:
Fri Oct 18, 2019 11:21 pm
rpdom wrote:
Fri Oct 18, 2019 5:48 am
If I get you right, I think you are trying to overcomplicate the code. Why not just use a set of if...elif...elif...else tests?

Code: Select all

while [something is true]:
    code
    code
    if [something happens]:
        do this
        do this
        do something else
    elif [something else happens]:
        do this
        do this
        do another thing
    elif [check for another thing]:
        do that
        do things
    else:
        do loads of other stuff
        all the other code
        everything here will be run
        only if the first two conditions
        are not met
    code here will be run regardless of the conditions
loop has ended
In my case "the rest of the code" does things that I don't want done. If a particular and specific condition is met, (the shooter rolls a "natural" (i.e. 12, 11, or 3), the rest of the loop should be skipped and the number of dice throws should not increment - since my game gives the shooter only so many dice rolls.
Well, my example does exactly that. The part of the code in the "else:" section is only run when the other "if" tests fail. So you end up with something like

Code: Select all

if roll = 3:
    do one thing
elif roll = 11:
    do another thing
elif roll = 12:
    do yet another thing
else:
   increment dice throw count
   and do everything else

User avatar
PeterO
Posts: 5084
Joined: Sun Jul 22, 2012 4:14 pm

Re: Continue *after* executing statements within a code block?

Sat Oct 19, 2019 9:06 am

jharris1993 wrote:
Fri Oct 18, 2019 11:21 pm
If you look at the examples shown in your link, along with every other example on Google, DogPile, DuckDuckGo, ad nauseum, you will notice that all of them, every single one, show the "continue" or "break" coming directly after the line that makes the test.
But none of them say there can't be statements between the conditional and the break or continue. If you had a better understanding of Python syntax you would know that. Maybe you need to work on that before attempting something beyond your current capabilities.

But you are not alone, it seems these days that actually learning a language's syntax before starting to use the language is unfashionable.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PiGraham
Posts: 3666
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Continue *after* executing statements within a code block?

Sat Oct 19, 2019 11:49 am

jharris1993 wrote:
Fri Oct 18, 2019 11:21 pm
If you look at the examples shown in your link, along with every other example on Google, DogPile, DuckDuckGo, ad nauseum, you will notice that all of them, every single one, show the "continue" or "break" coming directly after the line that makes the test.

Many, but not all, will show concise code, but none of those references say that is a restriction.

Since you made this point I provided an example that demonstrated that it is perfectly possible to put commands between the if and the continue.

jharris1993
Posts: 32
Joined: Sat Oct 05, 2013 10:26 pm

Re: Continue *after* executing statements within a code block?

Sat Oct 19, 2019 2:10 pm

Ok! Ok! Alright already!!

I bang my head on the floor in abject apology for daring to doubt my superiors. . . .

Code: Select all

# continue test

i=0
while i < 10:
    if i==4 or i==5:
        print("Oops!")
        i=i+1
        continue
    print(i)
    i=i+1
print(". . .end of line. . .")
produces

Code: Select all

Python 3.7.3 (/usr/bin/python3)
>>> %Run continue_test.py
0
1
2
3
Oops!
Oops!
6
7
8
9
. . .end of line. . .
>>>
Likewise, removing the "if true - continue" and replacing it with the continue after the previous statement worked in my craps game. This time. . . . . Though I'll be darned if I know why it didn't work before - and when it comes to programming, I HATE "magic".

Re: The code examples for "break" and "continue"
It has been my experience in the past that code examples in language specifications are usually both concise and exact. If there are "optional" parts of the example, they're usually explicitly specified.

Viz.:

Code: Select all

if [conditional expression]:
    {optional program statements}
    continue
If optional parts are not specified, they're not allowed. And since I did not find a single example of continue being used after any other program statements, then (silly me!) I figured that it wasn't allowed.
PeterO wrote: But you are not alone, it seems these days that actually learning a language's syntax before starting to use the language is unfashionable.
Huh? And where did you get that from what I said? Are you seriously expecting me - or anyone else - to sit down with the entire Python language specification and memorize it before trying to write any code? Sure, I read, than I try something. If that doesn't work, I read it again and then try something different. If that still doesn't work, I keep trying until I figure out what went wrong. If I still have trouble, then I ask. Which is what I did here.

Oh, and by the way. . . .
Have you ever seen a toddler trying to walk? The fellow stands, falls, stands again, falls again, and - eventually - gets the "standing" part working. Then he tries moving while standing - and falls again. He tries again, falls again, and so on until he's gotten reasonably good at walking.

If people never tried anything "beyond [their] current capabilities", they'd never accomplish anything.
Last edited by jharris1993 on Sat Oct 19, 2019 2:39 pm, edited 1 time in total.
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

Paul Hutch
Posts: 399
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: Continue *after* executing statements within a code block?

Sat Oct 19, 2019 2:15 pm

PiGraham wrote:
Sat Oct 19, 2019 11:49 am
Since you made this point I provided an example that demonstrated that it is perfectly possible to put commands between the if and the continue.
It may just be having hit 60 recently and haven't had my second cup of coffee this morning :) , but I got a little confused trying to follow the thread. Is this an accurate revision to your statement?
Since you made this point I added an example to my first post that demonstrated that it is perfectly possible to put commands between the if and the continue.

jharris1993
Posts: 32
Joined: Sat Oct 05, 2013 10:26 pm

Re: Continue *after* executing statements within a code block?

Sat Oct 19, 2019 4:05 pm

Paul Hutch wrote:
Sat Oct 19, 2019 2:15 pm
PiGraham wrote:
Sat Oct 19, 2019 11:49 am
Since you made this point I provided an example that demonstrated that it is perfectly possible to put commands between the if and the continue.
It may just be having hit 60 recently and haven't had my second cup of coffee this morning :) , but I got a little confused trying to follow the thread. Is this an accurate revision to your statement?
Since you made this point I added an example to my first post that demonstrated that it is perfectly possible to put commands between the if and the continue.
Yes you did, and this morning I tried my own "stub" test as well, to verify or disprove your conclusions, and it worked, damn it all!

The replacement code was close, but not exactly what I needed - though I really appreciated the effort. I greatly prefer constructive advice like this to ill-considered criticism.

Or, maybe I just don't understand British humor?
Jim "JR"

Some see things as they are, and ask "Why?"
I dream things that never were, and ask "Why Not".

Robert F. Kennedy

“Impossible” is only found in the dictionary of a fool.
Old Chinese Proverb

PiGraham
Posts: 3666
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Continue *after* executing statements within a code block?

Sat Oct 19, 2019 7:27 pm

Paul Hutch wrote:
Sat Oct 19, 2019 2:15 pm
PiGraham wrote:
Sat Oct 19, 2019 11:49 am
Since you made this point I provided an example that demonstrated that it is perfectly possible to put commands between the if and the continue.
It may just be having hit 60 recently and haven't had my second cup of coffee this morning :) , but I got a little confused trying to follow the thread. Is this an accurate revision to your statement?
Since you made this point I added an example to my first post that demonstrated that it is perfectly possible to put commands between the if and the continue.
I'm not sure what you mean. "Revision of your post"? Maybe you took "Since you..." to indicate something to do with order in time (at a later time or after a revision?) but I meant it the other common meaning of "for the reason that; because".

I didn't add an example later, I provided an example in my original posting.

Because it was mentioned I put it in my example.

Paul Hutch
Posts: 399
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: Continue *after* executing statements within a code block?

Sun Oct 20, 2019 11:38 am

Thank's PiGraham for confirming, it was just age and lack of caffeine causing my lack of thread following comprehension.

Return to “Python”