How to break while loop based on events raised in a thread (Python 2.7)

2024-11-27 Thread marc nicole via Python-list
e problem is the real time execution of the program (in class2) requires an instant check to be performed and that cannot wait until the current loop instructions finish, and I want is to break out of the outer while loop if the event is on without further checking any other condition. for n

Pythonic way to run the asyncio loop forever in python 3.10

2022-10-06 Thread David Jander
from outside the loop. While doing so, I have bumped against some common (I think) use-cases which seem to have no elegant solution anymore. At least I can't find one that seems "pythonic" or "correct". Problem: Before 3.10, asyncio.get_event_loop() was a convenient way to

Re: basic question on loop & type casting / list/str/array

2022-03-16 Thread Python
Kiran Kumar wrote: Hi. Pls check on below poython 3.9.x code & suggest how can i keep the string intactst in 2nd loop... ? these are aws ec2 ids Don't loop through it then. -- https://mail.python.org/mailman/listinfo/python-list

basic question on loop & type casting / list/str/array

2022-03-16 Thread Kiran Kumar
Hi. Pls check on below poython 3.9.x code & suggest how can i keep the string intactst in 2nd loop... ? these are aws ec2 ids >>> INSTANCE_ID = ['i-0dccf1ede229ce1','i-0285506fee62051'] >>> for i in INSTANCE_ID: ... print (i) ... i-0dccf1ede229ce1

Re: Python child process in while True loop blocks parent

2021-12-09 Thread Barry Scott
execv. The Python program > continues to run concurrently in a while True loop, and responds to requests > from C at intervals, and continues to run until it receives a signal from C > to exit. C sends signals to Python, then waits to receive data back from > Python. My problem was that

Re: Python child process in while True loop blocks parent

2021-12-08 Thread Peter J. Holzer
On 2021-12-08 18:11:48 +0100, Jen Kris via Python-list wrote: > To recap, I'm using a pair of named pipes for IPC between C and > Python.  Python runs as a child process after fork-execv.  The Python > program continues to run concurrently in a while True loop, and > responds to r

Re: Python child process in while True loop blocks parent

2021-12-08 Thread Cameron Simpson
On 08Dec2021 18:11, Jen Kris wrote: >Python must terminate its write strings with \n, or read will block in >C waiting for something that never comes. There are two aspects to this: - if upstream is rding "lines of text" then you need a newline to terminate the lines - you (probably) should f

Re: Python child process in while True loop blocks parent

2021-12-08 Thread Jen Kris via Python-list
nyone has a better solution I would appreciate any feedback.  To recap, I'm using a pair of named pipes for IPC between C and Python.  Python runs as a child process after fork-execv.  The Python program continues to run concurrently in a while True loop, and responds to requests from C at

Re: Python child process in while True loop blocks parent

2021-12-06 Thread Barry Scott
> On 6 Dec 2021, at 21:05, Jen Kris wrote: > > Here is what I don't understand from what you said. "The child process is > created with a single thread—the one that called fork()." To me that implies > that the thread that called fork() is the same thread as the child process. > I guess y

Re: Python child process in while True loop blocks parent

2021-12-06 Thread Jen Kris via Python-list
Here is what I don't understand from what you said.  "The child process is created with a single thread—the one that called fork()."  To me that implies that the thread that called fork() is the same thread as the child process.  I guess you're talking about the distinction between logical threa

Re: Python child process in while True loop blocks parent

2021-12-06 Thread Barry Scott
> On 6 Dec 2021, at 17:09, Jen Kris via Python-list > wrote: > > I can't find any support for your comment that "Fork creates a new > process and therefore also a new thread." From the Linux man pages > https://www.man7.org/linux/man-pages/man2/fork.2.html, "The child process is > created w

Re: Python child process in while True loop blocks parent

2021-12-06 Thread Chris Angelico
On Tue, Dec 7, 2021 at 4:10 AM Jen Kris via Python-list wrote: > > I can't find any support for your comment that "Fork creates a new > process and therefore also a new thread." From the Linux man pages > https://www.man7.org/linux/man-pages/man2/fork.2.html, "The child process is > created wit

Re: Python child process in while True loop blocks parent

2021-12-06 Thread Jen Kris via Python-list
I can't find any support for your comment that "Fork creates a new process and therefore also a new thread."  From the Linux man pages https://www.man7.org/linux/man-pages/man2/fork.2.html, "The child process is created with a single thread—the one that called fork()."  I have a one-core one-thr

Re: Python child process in while True loop blocks parent

2021-12-06 Thread Barry
 >> Thanks for your comments. >> >> I put the Python program on its own pthread, and call a small C program to >> fork-execv to call the Python program as a child process. > > What do you mean by putting python in it’s own pthread? > Are you embedding python

Re: Python child process in while True loop blocks parent

2021-12-05 Thread Peter J. Holzer
On 2021-12-06 00:51:13 +0100, Jen Kris via Python-list wrote: > The C program creates two threads (using pthreads), one for itself and > one for the child process.  On creation, the second pthread is pointed > to a C program that calls fork-execv to run the Python program.  That > way Python runs o

Re: Python child process in while True loop blocks parent

2021-12-05 Thread Jen Kris via Python-list
on program on its own pthread, and call a small C program to >> fork-execv to call the Python program as a child process.  >> > > What do you mean by putting python in it’s own pthread? > Are you embedding python in an other program? > > Barry > > > >> I re

Re: Python child process in while True loop blocks parent

2021-12-05 Thread Barry
own pthread? Are you embedding python in an other program? Barry > I revised the Python program to be a multiprocessing loop using the Python > multiprocessing module. That bypasses the GIL and allows Python to run > concurrently with C. So far so good. > > Next I will use Linux pip

Re: Python child process in while True loop blocks parent

2021-12-05 Thread Jen Kris via Python-list
Thanks for your comments.  I put the Python program on its own pthread, and call a small C program to fork-execv to call the Python program as a child process.  I revised the Python program to be a multiprocessing loop using the Python multiprocessing module.  That bypasses the GIL and allows

Re: Python child process in while True loop blocks parent

2021-12-04 Thread Barry Scott
that just does the fork and exec of a python program to test out your assumptions? If not I recommend that you do. Barry > > > > Nov 30, 2021, 11:42 by ba...@barrys-emacs.org: > > >> On 29 Nov 2021, at 22:31, Jen Kris > <mailto:jenk...@tutanota.com>> wrote: >>

Re: Python child process in while True loop blocks parent

2021-12-01 Thread Jen Kris via Python-list
> > > >> On 29 Nov 2021, at 22:31, Jen Kris <>> jenk...@tutanota.com>> > wrote: >> >> Thanks to you and Cameron for your replies.  The C side has an epoll_ctl >> set, but no event loop to handle it yet.  I'm putting that in now with a >

Re: Python child process in while True loop blocks parent

2021-11-30 Thread Barry Scott
> On 29 Nov 2021, at 22:31, Jen Kris wrote: > > Thanks to you and Cameron for your replies. The C side has an epoll_ctl set, > but no event loop to handle it yet. I'm putting that in now with a pipe > write in Python-- as Cameron pointed out that is the likely source

Re: Python child process in while True loop blocks parent

2021-11-29 Thread Jen Kris via Python-list
Thanks to you and Cameron for your replies.  The C side has an epoll_ctl set, but no event loop to handle it yet.  I'm putting that in now with a pipe write in Python-- as Cameron pointed out that is the likely source of blocking on C.  The pipes are opened as rdwr in Python because t

Re: Python child process in while True loop blocks parent

2021-11-29 Thread Barry
poll(). > > The Python child process is in a while True loop, which is intended to keep > it running while the parent process proceeds, and perform functions for the C > program only at intervals when the parent sends data to the child -- similar > to a daemon process. > >

Re: Python child process in while True loop blocks parent

2021-11-29 Thread Barry
poll(). > > The Python child process is in a while True loop, which is intended to keep > it running while the parent process proceeds, and perform functions for the C > program only at intervals when the parent sends data to the child -- similar > to a daemon process. > >

Re: Python child process in while True loop blocks parent

2021-11-29 Thread Cameron Simpson
in a while True loop, which is intended to keep it >running while the parent process proceeds, and perform functions for the C >program only at intervals when the parent sends data to the child -- similar >to a daemon process.  > >The C process writes to its end of the pipe and the

Python child process in while True loop blocks parent

2021-11-29 Thread Jen Kris via Python-list
I have a C program that forks to create a child process and uses execv to call a Python program.  The Python program communicates with the parent process (in C) through a FIFO pipe monitored with epoll().  The Python child process is in a while True loop, which is intended to keep it running

RE: sum() vs. loop

2021-10-13 Thread Avi Gross via Python-list
worry the original might be changed out from under. My apologies if it was understood to mean I had shown it was copied. -Original Message- From: Python-list On Behalf Of Stefan Ram Sent: Tuesday, October 12, 2021 9:49 PM To: python-list@python.org Subject: Re: sum() vs. loop &quo

Re: sum() vs. loop

2021-10-13 Thread Oscar Benjamin
On Mon, 11 Oct 2021 at 23:00, Christian Gollwitzer wrote: > > Am 10.10.21 um 10:49 schrieb Steve Keller: > > I have found the sum() function to be much slower than to loop over the > > operands myself: > > > > def sum_products(seq1, seq2): > > return s

Re: Re: sum() vs. loop

2021-10-12 Thread Chris Angelico
On Wed, Oct 13, 2021 at 12:36 PM Avi Gross via Python-list wrote: > > Alan, > > I am also wondering about that zip() function call to bind the two lists > into a sort of iterator object. Presumably that calls the iterator N times. > I did a test where I made two list called A and B and used zip to

RE: Re: sum() vs. loop

2021-10-12 Thread Avi Gross via Python-list
. -Original Message- From: Python-list On Behalf Of Alan Gauld Sent: Tuesday, October 12, 2021 6:56 PM To: python-list@python.org Subject: Fwd: Re: sum() vs. loop On 10/10/2021 09:49, Steve Keller wrote: > I have found the sum() function to be much slower than to loop over > the op

Re: sum() vs. loop

2021-10-12 Thread Christian Gollwitzer
Am 12.10.21 um 05:41 schrieb Dan Stromberg: On Mon, Oct 11, 2021 at 2:54 PM Steve Keller wrote: I have found the sum() function to be much slower than to loop over the operands myself: def sum_products(seq1, seq2): return sum([a * b for a, b in zip(seq1, seq2)]) def sum_products2(seq1

Fwd: Re: sum() vs. loop

2021-10-12 Thread Alan Gauld
On 10/10/2021 09:49, Steve Keller wrote: > I have found the sum() function to be much slower than to loop over the > operands myself: > > def sum_products(seq1, seq2): > return sum([a * b for a, b in zip(seq1, seq2)]) > > def sum_products2(seq1, seq2): > sum = 0 >

Re: sum() vs. loop

2021-10-11 Thread Dan Stromberg
On Mon, Oct 11, 2021 at 2:54 PM Steve Keller wrote: > I have found the sum() function to be much slower than to loop over the > operands myself: > > def sum_products(seq1, seq2): > return sum([a * b for a, b in zip(seq1, seq2)]) > > def sum_products2(seq1, seq2): >

Re: sum() vs. loop

2021-10-11 Thread Chris Angelico
ve 1000 entries each > (with float entries). > > However, in any case, the direct for-loop still is fastest. > Maybe, calling "next()" (actually, "PyIter_Next" in C) on > an iterator has some overhead compared to just evaluating > an expression in a loo

Re: sum() vs. loop

2021-10-11 Thread Chris Angelico
On Tue, Oct 12, 2021 at 8:55 AM Steve Keller wrote: > > I have found the sum() function to be much slower than to loop over the > operands myself: > > def sum_products(seq1, seq2): > return sum([a * b for a, b in zip(seq1, seq2)]) > > def sum_products2(seq1, seq2): &

Re: sum() vs. loop

2021-10-11 Thread Steve Keller
, of course. Much cleaner and I should have seen that myself. Thanks. BUT > (untested) ? I have tested it and with () instead of [] it's even slower: explicit loop: 37s ± .5s sum([...]) 44s ± .5s sum((...)) 47.5s ± .5s Now completely surprised. Steve -- https://mail.python.org/mailman/listinfo/python-list

Re: sum() vs. loop

2021-10-11 Thread Christian Gollwitzer
Am 10.10.21 um 10:49 schrieb Steve Keller: I have found the sum() function to be much slower than to loop over the operands myself: def sum_products(seq1, seq2): return sum([a * b for a, b in zip(seq1, seq2)]) def sum_products2(seq1, seq2): sum = 0 for a, b in zip(seq1, seq2

sum() vs. loop

2021-10-11 Thread Steve Keller
I have found the sum() function to be much slower than to loop over the operands myself: def sum_products(seq1, seq2): return sum([a * b for a, b in zip(seq1, seq2)]) def sum_products2(seq1, seq2): sum = 0 for a, b in zip(seq1, seq2): sum += a * b return sum In a program

Re: on writing a while loop for rolling two dice

2021-09-19 Thread Hope Rouselle
dn writes: [...] > Further, if you look at the OP's original solution, it only publishes > the last pair, ie the match, without mention of the list of non-matches. > Was it perhaps only a means of testing the solution? It was a means of showing the student that indeed they obtained a match. If

Re: on writing a while loop for rolling two dice

2021-09-11 Thread Andrew Jaffe
On 11/09/2021 10:09, dn via Python-list wrote: The stated requirement is: "I'd like to get the number of times I tried". Given such: why bother with returning any of the pairs of values? Indeed, if that's the requirement, then you can do even better, noting that the probability of getting a

Re: on writing a while loop for rolling two dice

2021-09-11 Thread Peter J. Holzer
On 2021-09-08 13:07:47 +1200, Greg Ewing wrote: > On 8/09/21 2:53 am, Grant Edwards wrote: > >#define IF if ( > >#define THEN ) { > >#define ELSE } else { > >#define ENDIF } > > I gather that early versions of some of the Unix utilities were > written by someone who liked using mac

Re: on writing a while loop for rolling two dice

2021-09-11 Thread dn via Python-list
On 11/09/2021 18.03, Chris Angelico wrote: > On Sat, Sep 11, 2021 at 3:26 PM dn via Python-list > wrote: >> >> On 31/08/2021 01.50, Chris Angelico wrote: >>> On Mon, Aug 30, 2021 at 11:13 PM David Raymond >>> wrote: > def how_many_times(): > x, y = 0, 1 > c = 0 > wh

Re: on writing a while loop for rolling two dice

2021-09-10 Thread Chris Angelico
On Sat, Sep 11, 2021 at 3:26 PM dn via Python-list wrote: > > On 31/08/2021 01.50, Chris Angelico wrote: > > On Mon, Aug 30, 2021 at 11:13 PM David Raymond > > wrote: > >> > >>> def how_many_times(): > >>> x, y = 0, 1 > >>> c = 0 > >>> while x != y: > >>> c = c + 1 > >>> x, y = rol

Re: on writing a while loop for rolling two dice

2021-09-10 Thread dn via Python-list
On 31/08/2021 01.50, Chris Angelico wrote: > On Mon, Aug 30, 2021 at 11:13 PM David Raymond > wrote: >> >>> def how_many_times(): >>> x, y = 0, 1 >>> c = 0 >>> while x != y: >>> c = c + 1 >>> x, y = roll() >>> return c, (x, y) >> >> Since I haven't seen it used in answers yet, her

Re: on writing a while loop for rolling two dice

2021-09-08 Thread Dennis Lee Bieber
On Wed, 8 Sep 2021 16:32:45 - (UTC), Grant Edwards declaimed the following: >On 2021-09-08, Dennis Lee Bieber wrote: > >> I spent close to 20 years (80s-90s) maintaining the /output/ of such >> a preprocessor. > >Ouch. I hope it paid well. ;) Only if one ignores the bloody cost of a

Re: on writing a while loop for rolling two dice

2021-09-08 Thread Dennis Lee Bieber
On Wed, 8 Sep 2021 14:46:28 - (UTC), Grant Edwards declaimed the following: >On 2021-09-08, charles hottel wrote: > >> So what do yoy think or feel about a language like RATFOR (Rational >> FORTRAN) which was implemented as macros? Should they instead have >> simply adapted themselves to

Re: on writing a while loop for rolling two dice

2021-09-08 Thread Grant Edwards
On 2021-09-08, Dennis Lee Bieber wrote: > I spent close to 20 years (80s-90s) maintaining the /output/ of such > a preprocessor. Ouch. I hope it paid well. ;) Back when I did a lot of TeX/LaTeX stuff on VMS, I used to make occasional fixes and add (very minor) features to one of the dvi handlin

RE: on writing a while loop for rolling two dice

2021-09-08 Thread Avi Gross via Python-list
customize Python to a point where it does things in fairly hidden ways too. There are no promises of purity. What this particular discussion began with was much lower level and about various ways of writing a while loop within very basic Python and seems to be heading away into discussions of other

Re: on writing a while loop for rolling two dice

2021-09-08 Thread Dennis Lee Bieber
On Wed, 8 Sep 2021 00:24:44 +0100, Alan Gauld via Python-list declaimed the following: > >That was quite common in C before it became popular(early/mid 80s). >I've seen Pascal, Algol and Coral macro sets in use. >You could even download pre-written ones from various >bulletin boards (remember the

Re: on writing a while loop for rolling two dice

2021-09-08 Thread Grant Edwards
On 2021-09-08, charles hottel wrote: > So what do yoy think or feel about a language like RATFOR (Rational > FORTRAN) which was implemented as macros? The RATFOR implementations I've seen weren't done using macros. It was a preprocessor, yes. But it generates code for the various structured sta

Re: on writing a while loop for rolling two dice

2021-09-08 Thread Grant Edwards
On 2021-09-08, charles hottel wrote: > So what do yoy think or feel about a language like RATFOR (Rational > FORTRAN) which was implemented as macros? Should they instead have > simply adapted themselves to FORTRAN? That's an interesting question. If the langauge is complete, well-defined, an

Re: on writing a while loop for rolling two dice

2021-09-08 Thread charles hottel
7, 2021 9:08 PM To: python-list@python.org Subject: Re: on writing a while loop for rolling two dice On 8/09/21 2:53 am, Grant Edwards wrote: #define IF if ( #define THEN ) { #define ELSE } else { #define ENDIF } ... I gather that early versions of some of the Unix utilities

RE: on writing a while loop for rolling two dice

2021-09-07 Thread Avi Gross via Python-list
another object means you over-rode the meaning of += for instance. -Original Message- From: Python-list On Behalf Of Richard Damon Sent: Tuesday, September 7, 2021 10:09 PM To: python-list@python.org Subject: Re: on writing a while loop for rolling two dice On 9/7/21 3:51 PM, Avi Gross via

Re: on writing a while loop for rolling two dice

2021-09-07 Thread Richard Damon
On 9/7/21 3:51 PM, Avi Gross via Python-list wrote: > and similarly changes any function imported directly > to also be fully qualified. One danger with this is that it can actual change the behavior of the program. Maybe more likely with global objects than functions, but still an issue. Remembe

RE: on writing a while loop for rolling two dice

2021-09-07 Thread Avi Gross via Python-list
@python.org Subject: Re: on writing a while loop for rolling two dice On 8/09/21 2:53 am, Grant Edwards wrote: >#define IF if ( >#define THEN ) { >#define ELSE } else { >#define ENDIF } >... I gather that early versions of some of the Unix utilities were written by so

Re: on writing a while loop for rolling two dice

2021-09-07 Thread Greg Ewing
On 8/09/21 2:53 am, Grant Edwards wrote: #define IF if ( #define THEN ) { #define ELSE } else { #define ENDIF } ... I gather that early versions of some of the Unix utilities were written by someone who liked using macros to make C resemble Algol. I guess you can get away with t

Re: on writing a while loop for rolling two dice

2021-09-07 Thread Alan Gauld via Python-list
On 07/09/2021 15:53, Grant Edwards wrote: > I remember engineering manager I worked with about 35 years ago who > used a set of C macros to try to make his code look as much like BASIC > as possible: > > #define IF if ( > #define THEN ) { > #define ELSE } else { > #define ENDIF } > ...

RE: on writing a while loop for rolling two dice

2021-09-07 Thread Avi Gross via Python-list
alister via Python-list Sent: Tuesday, September 7, 2021 2:58 PM To: python-list@python.org Subject: Re: on writing a while loop for rolling two dice On Tue, 07 Sep 2021 14:53:29 +, Grant Edwards wrote: > On 2021-09-06, Stefan Ram wrote: >> "Avi Gross" writes: >&g

Re: on writing a while loop for rolling two dice

2021-09-07 Thread Dennis Lee Bieber
On Tue, 7 Sep 2021 16:08:04 +1200, Greg Ewing declaimed the following: >On 7/09/21 11:38 am, Avi Gross wrote: > >> #define INDEFINITELY_LOOP while (true) >> >> So, how to do something like that in python, is a challenge left to the user >> ? > >def hell_frozen_over(): > return False > >whil

Re: on writing a while loop for rolling two dice

2021-09-07 Thread Hope Rouselle
Grant Edwards writes: > On 2021-09-06, Stefan Ram wrote: >> "Avi Gross" writes: >>> In languages like C/C++ there are people who make up macros like: >>>#define INDEFINITELY_LOOP while (true) >>>Or something like that and then allow the preprocessor to replace >>>INDEFINITELY_LOOP with valid C

Re: on writing a while loop for rolling two dice

2021-09-07 Thread alister via Python-list
On Tue, 07 Sep 2021 14:53:29 +, Grant Edwards wrote: > On 2021-09-06, Stefan Ram wrote: >> "Avi Gross" writes: >>> In languages like C/C++ there are people who make up macros like: >>>#define INDEFINITELY_LOOP while (true) >>>Or something like that and then allow the preprocessor to replace

Re: on writing a while loop for rolling two dice

2021-09-07 Thread Grant Edwards
On 2021-09-06, Stefan Ram wrote: > "Avi Gross" writes: >> In languages like C/C++ there are people who make up macros like: >>#define INDEFINITELY_LOOP while (true) >>Or something like that and then allow the preprocessor to replace >>INDEFINITELY_LOOP with valid C code. > > Those usually are

Re: on writing a while loop for rolling two dice

2021-09-07 Thread Greg Ewing
On 7/09/21 11:38 am, Avi Gross wrote: #define INDEFINITELY_LOOP while (true) So, how to do something like that in python, is a challenge left to the user 😉 def hell_frozen_over(): return False while not hell_frozen_over(): -- Greg -- https://mail.python.org/mailman/listinfo/py

RE: on writing a while loop for rolling two dice

2021-09-06 Thread Avi Gross via Python-list
ython-list@python.org Subject: Re: on writing a while loop for rolling two dice "Avi Gross" writes: > In languages like C/C++ there are people who make up macros like: >#define INDEFINITELY_LOOP while (true) >Or something like that and then allow the preprocessor to replace >IND

Non sequitur: on writing a while loop for rolling two dice

2021-09-06 Thread Dennis Lee Bieber
RESEND with clarification! On Mon, 6 Sep 2021 20:11:41 -0400, Avi Gross via Python-list declaimed the following: >changing. Ages ago we had code that processed MTA headers and every time we >had a meeting of standards bodies, we kept adding ever more headers and Why did my mind immedi

Non sequitur: on writing a while loop for rolling two dice

2021-09-06 Thread Dennis Lee Bieber
On Mon, 6 Sep 2021 20:11:41 -0400, Avi Gross via Python-list declaimed the following: >changing. Ages ago we had code that processed MTA headers and every time we >had a meeting of standards bodies, we kept adding ever more headers and Why did my mind immediately flash on "The Man Who Ne

RE: on writing a while loop for rolling two dice

2021-09-06 Thread Avi Gross via Python-list
(): print("repeating ...") The above gives me a nice infinite loop, with the second one looking like a normal loop but actually doing nothing much. -Original Message- From: Python-list On Behalf Of 2qdxy4rzwzuui...@potatochowder.com Sent: Monday, September 6, 2021 8:28 PM To:

Re: on writing a while loop for rolling two dice

2021-09-06 Thread 2QdxY4RzWzUUiLuE
On 2021-09-06 at 20:11:41 -0400, Avi Gross via Python-list wrote: > And in the python version, has anyone made a generator that returned > NULL or the like so you can say uselessly: > > for ( _ in forever() ) ... while "forever": ... -- https://mail.python.org/mailman/listinfo/pyth

RE: on writing a while loop for rolling two dice

2021-09-06 Thread Avi Gross via Python-list
are checking what types they are working with and perhaps converting to others or need to create objects with lots of dunders, can also make it error prone. Looking at our forever loop discussion, how often are the contents of the loop written with multiple terminating parts within the loop body

RE: on writing a while loop for rolling two dice

2021-09-06 Thread Avi Gross via Python-list
I actually like it if a language lets you spell out your intention, although adding many keywords is not a plus. So, yes something like: loop ... end loop; Is appealing as it makes clear the decision on when to exit the loop must be within the loop (or till

Re: on writing a while loop for rolling two dice

2021-09-06 Thread Dennis Lee Bieber
ion, consider languages with "repeat / until"... "do / while" repeats so long as the condition evaluates to "true"; "repeat / until" /exits/ when the condition evaluates to "true". Then... there is Ada... While

RE: on writing a while loop for rolling two dice

2021-09-06 Thread Avi Gross via Python-list
essage- From: Python-list On Behalf Of Hope Rouselle Sent: Thursday, September 2, 2021 10:42 AM To: python-list@python.org Subject: Re: on writing a while loop for rolling two dice David Raymond writes: >> def how_many_times(): >> x, y = 0, 1 >> c = 0 >> while x

Re: on writing a while loop for rolling two dice

2021-09-04 Thread Hope Rouselle
"Michael F. Stemper" writes: > On 04/09/2021 08.53, Hope Rouselle wrote: >> Chris Angelico writes: > >>> And at this point, it's looking pretty much identical to the for loop >>> version. Ultimately, they're all the same and you can pick a

Re: on writing a while loop for rolling two dice

2021-09-04 Thread Hope Rouselle
-start->8--- >> >> def how_many_times(): >> >> x, y = 0, 1 >> >> c = 0 >> >> while x != y: >> >> c = c + 1 >> >> x, y = roll() >> >> return c, (x, y) >> > >> >> >

Re: on writing a while loop for rolling two dice

2021-09-04 Thread Hope Rouselle
ed was the equal volume of work >> dedicated to while loops and for loops --- so I decided to compared >> which appeared more often in a certain sample of well-written Python >> code. It turns out the for loop was much more frequent. Students have >> been reporting too much

Re: on writing a while loop for rolling two dice

2021-09-04 Thread Michael F. Stemper
On 04/09/2021 08.53, Hope Rouselle wrote: Chris Angelico writes: And at this point, it's looking pretty much identical to the for loop version. Ultimately, they're all the same and you can pick and choose elements from each of them. I see. That's why C must have added th

Re: on writing a while loop for rolling two dice

2021-09-04 Thread Peter J. Holzer
s(): > >> x, y = 0, 1 > >> c = 0 > >> while x != y: > >> c = c + 1 > >> x, y = roll() > >> return c, (x, y) > > > >> > >> Why am I unhappy? I'm wish I could confine x, y to the while loop. > >>

Re: on writing a while loop for rolling two dice

2021-09-02 Thread lucas
def how_many_times(): x, y = 0, 1 c = 0 while x != y: c = c + 1 x, y = roll() return c, (x, y) Since I haven't seen it used in answers yet, here's another option using our new walrus operator def how_many_times(): roll_count = 1 while (rolls := roll())[0] != rol

Re: on writing a while loop for rolling two dice

2021-09-02 Thread Chris Angelico
k. If a student ever turns in an abomination like this, you can be extremely confident that it was copied from some programming site/list. Especially since I've used the two-arg version of iter() in there - that's quite a rarity. Hm. My mind is straying to evil things. The two-arg ite

Re: on writing a while loop for rolling two dice

2021-09-02 Thread Chris Angelico
ted to while loops and for loops --- so I decided to compared > which appeared more often in a certain sample of well-written Python > code. It turns out the for loop was much more frequent. Students have > been reporting too much work in too little time, so I decided to reduce > the n

Re: on writing a while loop for rolling two dice

2021-09-02 Thread Hope Rouselle
Chris Angelico writes: > On Mon, Aug 30, 2021 at 11:13 PM David Raymond > wrote: >> >> > def how_many_times(): >> > x, y = 0, 1 >> > c = 0 >> > while x != y: >> > c = c + 1 >> > x, y = roll() >> > return c, (x, y) >> >> Since I haven't seen it used in answers yet, here's another

Re: on writing a while loop for rolling two dice

2021-09-02 Thread Hope Rouselle
on using our > new walrus operator > > def how_many_times(): > roll_count = 1 > while (rolls := roll())[0] != rolls[1]: > roll_count += 1 > return (roll_count, rolls) That's nice, although it doesn't seem more readable to a novice seeing a while for the first ti

Re: on writing a while loop for rolling two dice

2021-09-02 Thread Hope Rouselle
c + 1 >> x, y = roll() >> return c, (x, y) > >> >> Why am I unhappy? I'm wish I could confine x, y to the while loop. The >> introduction of ``x, y = 0, 1'' must feel like a trick to a novice. How >> would you write this? > >>

Re: on writing a while loop for rolling two dice

2021-08-30 Thread Peter Otten
On 30/08/2021 06:17, dn via Python-list wrote: OTOH the simulation of rolling n-number of dice, as would happen in the real-world, would be broken by making the computer's algorithm more efficient (rolling until the first non-equal value is 'found'). Does that mean the realism of the model dies?

Re: on writing a while loop for rolling two dice

2021-08-30 Thread Chris Angelico
On Tue, Aug 31, 2021 at 12:28 AM Peter Otten <__pete...@web.de> wrote: > > On 30/08/2021 15:50, Chris Angelico wrote: > > > def how_many_times(): > > return next((count, rolls) for count, rolls in > > enumerate(iter(roll, None)) if len(Counter(rolls)) == 1) > > > That's certainly the most Coun

Re: on writing a while loop for rolling two dice

2021-08-30 Thread Peter Otten
On 30/08/2021 15:50, Chris Angelico wrote: def how_many_times(): return next((count, rolls) for count, rolls in enumerate(iter(roll, None)) if len(Counter(rolls)) == 1) That's certainly the most Counter-intuitive version so far;) Do I get bonus points for it being a one-liner that doe

Re: on writing a while loop for rolling two dice

2021-08-30 Thread Chris Angelico
On Mon, Aug 30, 2021 at 11:13 PM David Raymond wrote: > > > def how_many_times(): > > x, y = 0, 1 > > c = 0 > > while x != y: > > c = c + 1 > > x, y = roll() > > return c, (x, y) > > Since I haven't seen it used in answers yet, here's another option using our > new walrus operator

RE: on writing a while loop for rolling two dice

2021-08-30 Thread David Raymond
> def how_many_times(): > x, y = 0, 1 > c = 0 > while x != y: > c = c + 1 > x, y = roll() > return c, (x, y) Since I haven't seen it used in answers yet, here's another option using our new walrus operator def how_many_times(): roll_count = 1 while (rolls := roll())[0] !=

Re: on writing a while loop for rolling two dice

2021-08-29 Thread dn via Python-list
Each code-unit should do one job, and do it well! SRP... >> Efficiency: >> - wonder how max( d ) == min( d ) compares for speed with the set() type >> constructor? > > I did the simplest thing, speed was not a consideration. If it is, and > dies (sorry for that) is large I&#

Re: on writing a while loop for rolling two dice

2021-08-29 Thread Chris Angelico
On Mon, Aug 30, 2021 at 9:53 AM dn via Python-list wrote: > > On 29/08/2021 22.24, Chris Angelico wrote: > > On Sun, Aug 29, 2021 at 8:14 PM dn via Python-list > > wrote: > >> Efficiency: > >> - wonder how max( d ) == min( d ) compares for speed with the set() type > >> constructor? > > > > That

Re: on writing a while loop for rolling two dice

2021-08-29 Thread dn via Python-list
On 29/08/2021 22.24, Chris Angelico wrote: > On Sun, Aug 29, 2021 at 8:14 PM dn via Python-list > wrote: >> Efficiency: >> - wonder how max( d ) == min( d ) compares for speed with the set() type >> constructor? > > That may or may not be an improvement. > >> - alternately len( d ) < 2? >> - or

Re: on writing a while loop for rolling two dice

2021-08-29 Thread Peter Otten
for x in d) # don't mind one duplicate test For smaller numbers of dice I'd unpack (first, *rest) inside the for loop. But it's a trade-off, you' have to measure if/when it's better to go through the whole tuple in C. - alternately len( d ) < 2? - or len( d ) - 1

Re: on writing a while loop for rolling two dice

2021-08-29 Thread Chris Angelico
On Sun, Aug 29, 2021 at 8:14 PM dn via Python-list wrote: > Efficiency: > - wonder how max( d ) == min( d ) compares for speed with the set() type > constructor? That may or may not be an improvement. > - alternately len( d ) < 2? > - or len( d ) - 1 coerced to a boolean by the if? Neither of t

Re: on writing a while loop for rolling two dice

2021-08-29 Thread dn via Python-list
On 29/08/2021 20.06, Peter Otten wrote: ... > OK, maybe a bit complicated... but does it pay off if you want to > generalize? > def roll_die(faces): > while True: yield random.randrange(1, 1 + faces) > def hmt(faces, dies): > for c, d in enumerate(zip(*[roll_die(faces)]*dies), 1

Re: on writing a while loop for rolling two dice

2021-08-29 Thread Peter Otten
On 28/08/2021 14:00, Hope Rouselle wrote: def how_many_times(): x, y = 0, 1 c = 0 while x != y: c = c + 1 x, y = roll() return c, (x, y) --8<---cut here---end--->8--- Why am I unhappy? I'm wish I could confine x, y to the

RE: on writing a while loop for rolling two dice

2021-08-28 Thread Avi Gross via Python-list
And there is the ever popular recursive version you call with no while loop in sight. And, oddly, no variable declared in your main body: # CODE START --- import random def roll2(): return random.randint(1,6), random.randint(1,6) def roll_equal(counter): first, second = roll2

Re: on writing a while loop for rolling two dice

2021-08-28 Thread Alan Gauld via Python-list
t, I'm surprised ``outcome'' is still a valid name at the > return-statement. Wasn't it defined inside the while? If that really bugs you just replace the break with the return. >>> if outcome[ 0 ]== outcome[ 1 ]: >>>return roll_count, outco

Re: on writing a while loop for rolling two dice

2021-08-28 Thread Terry Reedy
= 0 while x != y: c = c + 1 x, y = roll() return c, (x, y) --8<---cut here---end--->8--- Why am I unhappy? I'm wish I could confine x, y to the while loop. The introduction of ``x, y = 0, 1'' must feel like a trick to a

Re: on writing a while loop for rolling two dice

2021-08-28 Thread dn via Python-list
x, y) > > Why am I unhappy? I'm wish I could confine x, y to the while loop. The > introduction of ``x, y = 0, 1'' must feel like a trick to a novice. How > would you write this? > r...@zedat.fu-berlin.de (Stefan Ram) writes: >> """Rolls two d

Re: on writing a while loop for rolling two dice

2021-08-28 Thread Hope Rouselle
Hope Rouselle writes: > r...@zedat.fu-berlin.de (Stefan Ram) writes: > >> Hope Rouselle writes: >>>How would you write this? >> >> """Rolls two dice until both yield the same value. >> Returns the number of times the two dice were rolled >> and the final value yielded.""" >> roll_count = 0 >> wh

  1   2   3   4   5   6   7   8   9   10   >