[issue45975] Simplify some while-loops with walrus operator

2022-02-03 Thread miss-islington


miss-islington  added the comment:


New changeset cf7cb1a2bf40516dc571d1d90c12b632dcd9b8c8 by Miss Islington (bot) 
in branch '3.9':
bpo-45975: IDLE - Remove extraneous parens (GH-31107)
https://github.com/python/cpython/commit/cf7cb1a2bf40516dc571d1d90c12b632dcd9b8c8


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-03 Thread miss-islington


miss-islington  added the comment:


New changeset 63523e7b2a631f28134b25a8063d50e08c741db6 by Miss Islington (bot) 
in branch '3.10':
bpo-45975: IDLE - Remove extraneous parens (GH-31107)
https://github.com/python/cpython/commit/63523e7b2a631f28134b25a8063d50e08c741db6


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-03 Thread miss-islington


Change by miss-islington :


--
pull_requests: +29293
pull_request: https://github.com/python/cpython/pull/31110

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-03 Thread miss-islington


Change by miss-islington :


--
pull_requests: +29292
pull_request: https://github.com/python/cpython/pull/31109

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-03 Thread Terry J. Reedy


Terry J. Reedy  added the comment:


New changeset 916d0d822c79933f4c420f7a36f16f3eb788646b by Terry Jan Reedy in 
branch 'main':
bpo-45975: IDLE - Remove extraneous parens (GH-31107)
https://github.com/python/cpython/commit/916d0d822c79933f4c420f7a36f16f3eb788646b


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-03 Thread Terry J. Reedy


Change by Terry J. Reedy :


--
pull_requests: +29290
pull_request: https://github.com/python/cpython/pull/31107

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-02 Thread Terry J. Reedy


Terry J. Reedy  added the comment:


New changeset fafd2dadf63973a04f5693e5be19f3e7521c10d4 by Terry Jan Reedy in 
branch '3.9':
[3.9] bpo-45975: Use walrus operator for some idlelib while loops (GH-31083)
https://github.com/python/cpython/commit/fafd2dadf63973a04f5693e5be19f3e7521c10d4


--

___
Python tracker 
<https://bugs.python.org/issue45975>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-02 Thread Terry J. Reedy


Change by Terry J. Reedy :


--
pull_requests: +29277
pull_request: https://github.com/python/cpython/pull/31092

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-02 Thread miss-islington


miss-islington  added the comment:


New changeset 2ddc278875f789de622262ee8ff5a1c3788f031c by Miss Islington (bot) 
in branch '3.10':
bpo-45975: Use walrus operator for some idlelib while loops (GH-31083)
https://github.com/python/cpython/commit/2ddc278875f789de622262ee8ff5a1c3788f031c


--

___
Python tracker 
<https://bugs.python.org/issue45975>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-02 Thread miss-islington


Change by miss-islington :


--
nosy: +miss-islington
nosy_count: 6.0 -> 7.0
pull_requests: +29276
pull_request: https://github.com/python/cpython/pull/31091

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-02 Thread Terry J. Reedy


Terry J. Reedy  added the comment:


New changeset 51a95be1d035a717ab29e98056b8831a98e61125 by Nick Drozd in branch 
'main':
bpo-45975: Use walrus operator for some idlelib while loops (GH-31083)
https://github.com/python/cpython/commit/51a95be1d035a717ab29e98056b8831a98e61125


--

___
Python tracker 
<https://bugs.python.org/issue45975>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-02-02 Thread Nick Drozd


Change by Nick Drozd :


--
keywords: +patch
pull_requests: +29267
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/31083

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2022-01-26 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

To me, the five idlelib changes make the code easier to understand.  Nick, 
please either move them into a separate PR or allow me to do so, and subject to 
manual testing, I will merge and backport.  (I requested this on the PR by 
maybe you missed it.)

If there are other plausible changes in idlelib that you would like considered, 
make a spinoff issue where we can discuss them first.

--
nosy: +terry.reedy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2021-12-04 Thread Alex Waygood


Change by Alex Waygood :


--
nosy: +AlexWaygood

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2021-12-04 Thread Nick Drozd


Nick Drozd  added the comment:

> Is there any measurable performance benefit from this?

I wouldn't expect any performance changes either way. If it worked out to be 
slower, that would an unpleasant surprise and a good reason to reject this 
change. If it worked out to be faster, well, that would be great!

> Reducing several hundred thousand lines of code by 148 is not a compelling 
> benefit :)

But it's not just any old 148 lines. By my count it includes the removal of 47 
`break` statements. For a change of this nature there's certainly a chance of 
introducing errors. On the other hand, every one of those `break` statements is 
a site of manual loop-handling logic, and those already present some risk of 
introducing errors.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2021-12-04 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

> As I mentioned in the PR, merging this can make future
> backports to 3.6 and 3.7 more complicated.

That's correct, but it is also true that we do very few of those and the 
likelihood of a conflict with one of these edits is low.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2021-12-04 Thread Irit Katriel


Irit Katriel  added the comment:

As I mentioned in the PR, merging this can make future backports to 3.6 and 3.7 
more complicated.

--
nosy: +iritkatriel

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2021-12-03 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

The general rule as stated by Zachary is correct; however, I'm in mildly favor 
of this PR because these are the use cases that the walrus operator was 
specifically designed for.  That said, it would be nice to verify that timings 
don't get worse and to carefully review each edit to make sure it doesn't 
introduce a bug.

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2021-12-03 Thread Zachary Ware


Zachary Ware  added the comment:

As a general rule, refactorings like this tend to be rejected as the risk of 
inadvertently adding a new bug outweighs the benefit of subjectively cleaner 
code ("if it ain't broke, don't fix it!").

Is there any measurable performance benefit from this? Reducing several hundred 
thousand lines of code by 148 is not a compelling benefit :)

--
nosy: +zach.ware

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45975] Simplify some while-loops with walrus operator

2021-12-03 Thread Nick Drozd


New submission from Nick Drozd :

The following pattern occurs a few times in the codebase:

  while 1:
  data = conn.recv(blocksize)
  if not data:
  break
  ...

There is an unbounded while loop in which some kind of input is read. If the 
input is there, it is processed and the loop is continued; otherwise the loop 
is broken.

This can be expressed more cleanly with the walrus operator:

  while data := conn.recv(blocksize):
 ...

Some of this code goes back to the days of Python 1. I assume that the original 
authors would have used the walrus idiom if it had been available, which it 
wasn't. But now it is.

Rewriting the instances of this pattern shaves off 148 lines from the codebase. 
Removing the manual break statements makes the logic more straightforward.

This should not change the behavior of anything. I'm assuming that this code is 
all under test and that any deviations from the existing behavior will be 
caught. Anything that isn't tested shouldn't be changed (and should be tested).

--
components: Library (Lib)
messages: 407615
nosy: nickdrozd
priority: normal
pull_requests: 28133
severity: normal
status: open
title: Simplify some while-loops with walrus operator
versions: Python 3.11

___
Python tracker 
<https://bugs.python.org/issue45975>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36066] Add `empty` block to `for` and `while` loops.

2019-02-28 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

Syntax changes need PEPs.  By default, syntax changes are rejected.  This is 
especially true for new keywords, as any acceptible word will already be in 
use, and hence its introduction *will* break code.

We are still having issues over having temporarily made 'async' and 'await' 
'semi-keywords' (for 2 versions) to smooth out breakage.  So new keywords must 
enable something previously difficult.  But implementing the 'empty' idea is 
fairly trivial, as demonstrated in the initial post.

A third alternative is a peak(iterator) class with a __bool__ method.  I posted 
one once, probably on python-list.  But the extra overhead every iteration must 
be larger than the simple assignment, which is so fast one should not worry 
about it.  I suspect the flag also beats the chain alternative.
---

[More opinionated than the above ...]

Calling what is easy and works today 'non-idiomatic' is nearly pure opinion and 
not persuasive as a rhetorical device.

The complicated rules for else-never interaction to be far more 
'anti-idiomatic' from a comprehension point of view.  In any case, a flag 
allows easy, flexible, and precise ordering of never, something, and either-way 
code.

--
nosy: +terry.reedy
resolution:  -> postponed
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36066] Add `empty` block to `for` and `while` loops.

2019-02-22 Thread WloHu


WloHu  added the comment:

Sorry for beeing ignorant about python-ideas. The linked proposal is almost 
exatly what I've requested here, the differences are that it discusses other 
keyword alternatives and mine suggests an alternative for making empty 
generators false.

If lack of follow-up in the linked proposal can be considered as "won't do" 
then this issue can be closed. Should I repost any part of this to python-ideas 
or whole case is closed?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36066] Add `empty` block to `for` and `while` loops.

2019-02-21 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Slightly similar proposal in the past : 
https://mail.python.org/pipermail/python-ideas/2016-March/038897.html

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36066] Add `empty` block to `for` and `while` loops.

2019-02-21 Thread Stéphane Wirtel

Stéphane Wirtel  added the comment:

This issue should be discussed on python-ideas

https://mail.python.org/mailman/listinfo/python-ideas

--
nosy: +matrixise

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36066] Add `empty` block to `for` and `while` loops.

2019-02-21 Thread Stéphane Wirtel

Stéphane Wirtel  added the comment:

yep and there was no followup. maybe we could close this issue.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36066] Add `empty` block to `for` and `while` loops.

2019-02-21 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

I would recommend posting this on python-ideas to get some feedback. This 
introduces new control flow and breaks some old assumptions as in third case 
empty block is executed and there might be code that depends upon current 
for-else behavior where else should be executed. Also reading the examples 
initially this seems to add little cognitive overhead too since there is now 
empty and else that are executed based on empty iterable, breaking out of the 
loop, natural ending of the loop which might make this little hard to teach too.

--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue36066] Add `empty` block to `for` and `while` loops.

2019-02-21 Thread WloHu


New submission from WloHu :

###
Description

Adding `empty` block to loops will extend them to form for-empty-else and 
while-empty-else. The idea is that `empty` block will execute when loop 
iteration wasn't performed because iterated element was empty. The idea is 
taken from Django framework' `{% empty %}` block 
(https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#for-empty).

###
Details

There are combinations how this loop should work together with `else` block 
(`for` loop taken as example):
1. for-empty - `empty` block runs when iteration wasn't performed, i.e. ended 
naturally because of empty iterator;
2. for-else - `else` block runs when iteration ended naturally either because 
iterator was empty or exhausted, behavior the same as currently implemented;
3. for-empty-else - in this form there is split depending on the way in which 
loop ended naturally:
-- empty iterator - only `empty` block is executed,
-- non-empty iterator - only `else` block is executed.

In 3rd case `else` block is not executed together with `empty` block because 
this can be done by using for-else form. The only reason to make this case work 
differently is code duplication in case when regardless of the way, in which 
loop ended naturally, there is common code we want to execute. E.g.:
```
for:
...
empty:
statement1
statement2
else:
statement1
statement3
```

However implementing the "common-avoid-duplication" case will be inconsisted 
with `try-except` which executes only 1st matching `except` block.

###
Current alternative solutions

In case when iterable object works well with "empty test" (e.g.: `list`, `set`) 
the most simple solution is:
```
if iterable:
print("Empty")
else:
for item in iterable:
print(item)
else:
print("Ended naturally - non-empty.")
```

Which looks good and is simple enough to avoid extending the language. However 
in general this would fail if `iterable` object is a generator which is always 
truthy and fails the expectations of "empty test".
In such case special handling should be made to make it work in general. So far 
I see 3 options:
- use helper variable `x = list(iterable)` and do "empty test" as shown above - 
this isn't an option for unbound `iterable` like stream or asynchronous message 
queue;
- test generator for emptiness a.k.a. peek next element:
```
try:
first = next(iterable)
except StopIteration:
print("Empty")
else:
for item in itertools.chain([first], iterable):
print(item)
else:
print("Ended naturally - non-empty.")
```

- add `empty` flag inside loop:
```
empty = True
for item in iterable:
empty = False  # Sadly executed for each `item`.
print(item)
else:
if empty:
print("Empty")
else
print("Ended naturally - non-empty.")
```

The two latter options aren't really idiomatic compared to proposed:
```
for item in iterable:
print(item)
empty:
print("Empty")
else:
print("Ended naturally - non-empty.")
```

###
Enchancement pros and cons
Pros:
- more idiomatic solution to handle natural loop exhaustion for empty iterator,
- shorter horizontal indentation compared to current alternatives,
- quite consistent flow control splitting compared to `try-except`,
- not so exotic as it's already implemented in Django (`{% empty %}`) and 
Jinja2 (`{% else %}`).

Cons:
- new keyword/token,
- applies to even smaller number of usecases than for-else which is still 
considered exotic.

###
Actual (my) usecase (shortened):
```
empty = True
for message in messages:
empty = False
try:
decoded = message.decode()
except ...:
...
... # Handle different exception types.
else:
log.info("Success")
break
else:
if empty:
error_message = "No messages."
else:
error_message = "Failed to decode available messages."
log.error(error_message)
```

###
One more thing to convince readers

Considering that Python "went exotic" with for-else and while-else to solve `if 
not_found: print('Not found.')` case, adding `empty` seems like next inductive 
step in controling flow of loops.

###
Alternative solution

Enhance generators to work in "empty test" which peeks for next element behind 
the scenes. This will additionally solve annoying issue for testing empty 
generators, which currently must be handled as special case of iterable object. 
Moreover this solution doesn't require new language keywords.

--
components: Interpreter Core
messages: 336221
nosy: wlohu
priority: normal
severity: normal
status: open
title: Add `empty` block to `for` and `while` loops.
type: enhancement
versions: Python 3.7

___
Python tracker 
<https://bugs.python.org/issue36066>

Re: Break and Continue: While Loops

2016-06-26 Thread Elizabeth Weiss
On Thursday, June 23, 2016 at 12:17:23 AM UTC-4, Elizabeth Weiss wrote:
> CODE #1:
> 
> i=0
> while 1==1:
>print(i)
>i=i+1
>if i>=5:
>  print("Breaking")
>  break
> 
> --
> I understand that i=0 and i will only be printed if 1=1
> The results of this is
> 0
> 1
> 2
> 3
> 4
> Breaking
> 
> Why is Breaking going to be printed if i only goes up to 4? It does say if 
> i>=5? Shouldn't this mean that the results should be:
> 0
> 1
> 2
> 3
> 4
> 5
> 
> CODE #2:
> 
> i=0
> while True:
>i=i+1
>   if i==2:
>  print("Skipping 2")
>  continue
>   if i==5:
>  print("Breaking")
>  break
>print(i)
> 
> --
> 
> Questions:
> 1. what does the word True have to do with anything here? 
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> 4. How is i equal to 2 or 5 if i=0?
> 
> Thanks for all of your help!

Thank you, everyone!!! It all makes sense now!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-26 Thread Elizabeth Weiss
On Thursday, June 23, 2016 at 1:20:37 AM UTC-4, DFS wrote:
> On 6/23/2016 12:17 AM, Elizabeth Weiss wrote:
> 
> > CODE #1:
> >
> > i=0
> > while 1==1:
> >print(i)
> >i=i+1
> >if i>=5:
> >  print("Breaking")
> >  break
> >
> > --
> > I understand that i=0 and i will only be printed if 1=1
> > The results of this is
> > 0
> > 1
> > 2
> > 3
> > 4
> > Breaking
>   >
> > Why is Breaking going to be printed if i only goes up to 4? It does say if 
> > i>=5? Shouldn't this mean that the results should be:
> 
> i is incremented up to 5, but you're printing i before incrementing and 
> evaluating it.
> 
> Move i=i+1 to the end and try it again.
> 
> 
> 
> 
> 
> > CODE #2:
> >
> > i=0
> > while True:
> >i=i+1
> >   if i==2:
> >  print("Skipping 2")
> >  continue
> >   if i==5:
> >  print("Breaking")
> >  break
> >print(i)
> >
> > --
> >
> > Questions:
> > 1. what does the word True have to do with anything here?
> 
> 'True' is the expression that's evaluated on each iteration of the While 
> loop.
> 
> while True:
> print('True')
> 
> while 1:
> print('True')
> 
> while 1==1:
> print('True')
> 
> are all equivalent, and will run indefinitely, because the statements 
> after the while expression do nothing to stop the loop.
> 
> 
> 
> 
> > 2. i=i+1- I never understand this. Why isn't it i=i+2?
> 
> i=i+1 adds 1 to the current value of i, then assigns that value to i.
> 
> You can increment i by whatever you want:
> i=i+1
> i=i+2
> i=i+35
> etc.
> 
> 
> 
> 
> > 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> 
> The print results don't include 2 or 5 because you said 'continue' if 2 
> and 'break' if 5.  So you skipped over 2, and exited at 5.
> 
> 
> 
> 
> > 4. How is i equal to 2 or 5 if i=0?
> 
> i=0 before you entered the While loop.  Then you increment it by one 
> (i=i+1) on each iteration of the loop.
> 
> 
> 
> > Thanks for all of your help!
> 
> 
> Also, there's a million python resources on the web.
> 
> http://www.pythonschool.net/basics/while-loops/

Got it! Thank you!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-26 Thread Elizabeth Weiss
On Thursday, June 23, 2016 at 1:06:09 AM UTC-4, Rustom Mody wrote:
> On Thursday, June 23, 2016 at 9:47:23 AM UTC+5:30, Elizabeth Weiss wrote:
> > CODE #1:
> > 
> > i=0
> > while 1==1:
> >print(i)
> >i=i+1
> >if i>=5:
> >  print("Breaking")
> >  break
> > 
> > --
> > I understand that i=0 and i will only be printed if 1=1
> > The results of this is
> > 0
> > 1
> > 2
> > 3
> > 4
> > Breaking
> > 
> > Why is Breaking going to be printed if i only goes up to 4? It does say if 
> > i>=5? Shouldn't this mean that the results should be:
> > 0
> > 1
> > 2
> > 3
> > 4
> > 5
> > 
> > CODE #2:
> > 
> > i=0
> > while True:
> >i=i+1
> >   if i==2:
> >  print("Skipping 2")
> >  continue
> >   if i==5:
> >  print("Breaking")
> >  break
> >print(i)
> > 
> > --
> > 
> > Questions:
> > 1. what does the word True have to do with anything here? 
> > 2. i=i+1- I never understand this. Why isn't it i=i+2?
> > 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> > 4. How is i equal to 2 or 5 if i=0?
> > 
> > Thanks for all of your help!
> 
> I suggested in your other post (Subject While Loops)
> that the predecessor of python ABC's syntax for assignment would help 
> unconfuse you
> 
> ie the ASSIGNMENT x=y we write as PUT y IN x
> 
> Using that rewrite your CODE 1 as
> 
> PUT 0 in i
> while 1==1:
>print(i)
>PUT i+1 IN i
>if i>=5:
>  print("Breaking")
>  break 
> 
> Now try and rethink what that does
> Then repeat for your other examples that confuse

Got it! Thank you!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-26 Thread Elizabeth Weiss
On Thursday, June 23, 2016 at 12:49:30 AM UTC-4, Lawrence D’Oliveiro wrote:
> On Thursday, June 23, 2016 at 4:17:23 PM UTC+12, Elizabeth Weiss wrote:
> > 
> > i=0
> > while 1==1:
> >print(i)
> >i=i+1
> >if i>=5:
> >  print("Breaking")
> >  break
> > 
> > Why is Breaking going to be printed if i only goes up to 4? It does say if
> > i>=5?
> 
> Because you incremented i after printing its value, and before checking it.

Got it, Lawrence. Thanks so much for your help!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-26 Thread Steven D'Aprano
On Sun, 26 Jun 2016 05:32 pm, Lawrence D’Oliveiro wrote:

> On Thursday, June 23, 2016 at 11:58:01 PM UTC+12, Jon Ribbens wrote:
>> I seem to recall that Java originally insisted that only booleans
>> (excluding even Booleans, which are a different thing because of
>> course they are) could be checked for truth and it was one of
>> Java's significant warts.
> 
> Java’s wart was it didn’t implement Booleans the way they were done in
> Pascal. That wouldn’t have been a wart.

Oh? How are Java booleans different from Pascal booleans?

Are you referring to "boxed" booleans, i.e. native bools in an object
wrapper?





-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-26 Thread Lawrence D’Oliveiro
On Thursday, June 23, 2016 at 11:58:01 PM UTC+12, Jon Ribbens wrote:
> I seem to recall that Java originally insisted that only booleans
> (excluding even Booleans, which are a different thing because of
> course they are) could be checked for truth and it was one of
> Java's significant warts.

Java’s wart was it didn’t implement Booleans the way they were done in Pascal. 
That wouldn’t have been a wart.

Python’s Boolean handling is a wart in the other direction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-24 Thread Pierre-Alain Dorange
BartC  wrote:

> But even with ordinary conditionals, False is False, but [False] is 
> True. And [] is False, while [[]] is True. A class instance is always
> True, even when empty. And then "False" is True as well!

"Empty" is not "Nothing". To be empty, something must exist first.

-- 
Pierre-Alain Dorange   Moof 

Ce message est sous licence Creative Commons "by-nc-sa-2.0"

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-23 Thread John Gordon
In <639b00e0-7b9d-4ed4-96ad-6afbcd536...@googlegroups.com> Elizabeth Weiss 
 writes:

> i=0
> while 1==1:
>print(i)
>i=i+1
>if i>=5:
>  print("Breaking")
>  break

> Why is Breaking going to be printed if i only goes up to 4?

Your code prints i and THEN adds one to it.

So i is 4, it gets printed, then 1 is added to it, so it becomes 5
and then the loop exits.

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-23 Thread Chris Angelico
On Fri, Jun 24, 2016 at 1:52 AM, BartC  wrote:
> On 23/06/2016 12:39, Chris Angelico wrote:
>>
>> On Thu, Jun 23, 2016 at 8:15 PM, BartC  wrote:
>>>
>>> Actually pretty much any expression can be used, because Python can
>>> interpret almost anything as either True or False. Don't ask for the
>>> rules
>>> because they can be complicated, but for example, zero is False, and any
>>> other number is True. I think.
>>
>>
>> The rules are very simple. Anything that represents "something" is
>> true, and anything that represents "nothing" is false. An empty
>> string, an empty list, an empty set, and the special "None" object
>> (generally representing the absence of some other object) are all
>> false. A string with something in it (eg "Hello"), a list with
>> something in it (eg [1,2,4,8]), etc, etc, are all true.
>
>
> But even with ordinary conditionals, False is False, but [False] is True.
> And [] is False, while [[]] is True. A class instance is always True, even
> when empty. And then "False" is True as well!

[False] is a list with something in it. So is [[]]. "False" is a
string with something in it, and honestly, if you think that ought to
come out falsey, you're asking for magic. I don't know what you mean
by a "class instance", or how it would be empty, but if you're
creating a custom class and it might sometimes represent emptiness,
you need to define either __len__ or __bool__ so the interpreter can
understand what you mean by "empty".

> Maybe I was thinking of 'is' where sometimes the results are unintuitive.
>

Not at all. The 'is' operator tells you whether the left and right
operands are the same object. That's it. Maybe you have unintuitive
situations where you don't realize you have the same object (or don't
realize you have different objects), but the operator itself still has
one very simple rule to follow.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-23 Thread BartC

On 23/06/2016 12:39, Chris Angelico wrote:

On Thu, Jun 23, 2016 at 8:15 PM, BartC  wrote:

Actually pretty much any expression can be used, because Python can
interpret almost anything as either True or False. Don't ask for the rules
because they can be complicated, but for example, zero is False, and any
other number is True. I think.


The rules are very simple. Anything that represents "something" is
true, and anything that represents "nothing" is false. An empty
string, an empty list, an empty set, and the special "None" object
(generally representing the absence of some other object) are all
false. A string with something in it (eg "Hello"), a list with
something in it (eg [1,2,4,8]), etc, etc, are all true.


Maybe I was thinking of 'is' where sometimes the results are unintuitive.

But even with ordinary conditionals, False is False, but [False] is 
True. And [] is False, while [[]] is True. A class instance is always 
True, even when empty. And then "False" is True as well!


--
Bartc

--
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-23 Thread Jon Ribbens
On 2016-06-23, Chris Angelico  wrote:
> On Thu, Jun 23, 2016 at 8:15 PM, BartC  wrote:
>> Actually pretty much any expression can be used, because Python can
>> interpret almost anything as either True or False. Don't ask for the rules
>> because they can be complicated, but for example, zero is False, and any
>> other number is True. I think.
>
> The rules are very simple. Anything that represents "something" is
> true, and anything that represents "nothing" is false. An empty
> string, an empty list, an empty set, and the special "None" object
> (generally representing the absence of some other object) are all
> false. A string with something in it (eg "Hello"), a list with
> something in it (eg [1,2,4,8]), etc, etc, are all true.

Exactly. This is a major everyday strength of Python in my view.

I seem to recall that Java originally insisted that only booleans
(excluding even Booleans, which are a different thing because of
course they are) could be checked for truth and it was one of
Java's significant warts.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-23 Thread Chris Angelico
On Thu, Jun 23, 2016 at 8:15 PM, BartC  wrote:
> Actually pretty much any expression can be used, because Python can
> interpret almost anything as either True or False. Don't ask for the rules
> because they can be complicated, but for example, zero is False, and any
> other number is True. I think.

The rules are very simple. Anything that represents "something" is
true, and anything that represents "nothing" is false. An empty
string, an empty list, an empty set, and the special "None" object
(generally representing the absence of some other object) are all
false. A string with something in it (eg "Hello"), a list with
something in it (eg [1,2,4,8]), etc, etc, are all true.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-23 Thread BartC

On 23/06/2016 05:17, Elizabeth Weiss wrote:

CODE #1:



i=0
while True:
   i=i+1
  if i==2:
 print("Skipping 2")
 continue
  if i==5:
 print("Breaking")
 break
   print(i)

--

Questions:
2. i=i+1- I never understand this. Why isn't it i=i+2?
3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
4. How is i equal to 2 or 5 if i=0?


i=0 doesn't mean that 'i' is equal to 0.

In Python, "=" means 'assignment'. Something more like this:

  SET i to 0

i=i+1 means:

  SET i to i+1

'i' is a name for some value. The value of 'i' can change, usually 
through assignments. If 'i' currently has the value 37, then:


  SET i to i+1

will now store i+1 (ie. 38) into 'i'. Repeating that will set 'i' to 39 
and so on.


To test if 'i' has a certain value, then '==' is used (to distinguish it 
from '=' which means SET).


So i==37 will give True if i currently has a value of 37, or False 
otherwise. This is used in your code:


  if i==2:
 print ("Skipping 2")
 continue

The indented lines are executed when i has the value 2.

> 1. what does the word True have to do with anything here?

The 'while' statement (like others such as 'if') takes an expression 
that should give True or False. If True, the loop is executed once more.


Actually pretty much any expression can be used, because Python can 
interpret almost anything as either True or False. Don't ask for the 
rules because they can be complicated, but for example, zero is False, 
and any other number is True. I think.


Anyway, your code is doing an endless loop. Python doesn't have a 
dedicated statement for an endless loop, so you have to use 'while True' 
or 'while 1'.


(Well, the loop is only endless until you do an explicit exit out of it, 
such as using 'break'.)


--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-23 Thread alister
On Wed, 22 Jun 2016 21:17:03 -0700, Elizabeth Weiss wrote:

> CODE #1:
> 
> i=0 while 1==1:
>print(i)
>i=i+1 if i>=5:
>  print("Breaking") break
> 
> --
> I understand that i=0 and i will only be printed if 1=1 The results of
> this is 0
> 1
> 2
> 3
> 4
> Breaking
> 
> Why is Breaking going to be printed if i only goes up to 4? It does say
> if i>=5? Shouldn't this mean that the results should be:
> 0
> 1
> 2
> 3
> 4
> 5
> 
> CODE #2:
> 
> i=0 while True:
>i=i+1
>   if i==2:
>  print("Skipping 2")
>  continue
>   if i==5:
>  print("Breaking") break
>print(i)
> 
> --
> 
> Questions:
> 1. what does the word True have to do with anything here?
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if
> i==5?
> 4. How is i equal to 2 or 5 if i=0?
> 
> Thanks for all of your help!

unrelated to your question

while 1==1

Don't do this.

code 2 you correctly use while True

to see the problem with your code
try following it through line by line with a pen & paper
writing the value for I each time it is changed.
(this is known in the trade as a "Dry Run")





-- 
Fame is a vapor; popularity an accident; the only earthly certainty is
oblivion.
-- Mark Twain
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-23 Thread Steven D'Aprano
On Thursday 23 June 2016 14:17, Elizabeth Weiss wrote:

> CODE #2:
> 
> i=0
> while True:
>i=i+1
>   if i==2:
>  print("Skipping 2")
>  continue
>   if i==5:
>  print("Breaking")
>  break
>print(i)
> 
> --
> 
> Questions:
> 1. what does the word True have to do with anything here?

Consider:

"while the pasta is too hard to eat, keep boiling it"

"while you are feeling sick, stay in bed and drink plenty of fluids"

"While" repeatedly takes a condition, decides if it is true or false, and then 
decides what to do. If you give it a condition True, that's *always* true, so 
it repeats forever unless you use "break" to escape from the loop.


> 2. i=i+1- I never understand this. Why isn't it i=i+2?

i = i + 1 increase i by one each time around the loop:

i = 0, then 1, then 2, then 3, then 4...

If you used i = i+2, it would increase by two each time around the loop:

i = 0, then 2, then 4, then 6, then ...


> 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> 4. How is i equal to 2 or 5 if i=0?

i starts off as 0. But then you increase it by 1 each time around the loop.


Can I ask, have you learned about for loops yet? There seems to be a fashion 
among some teachers and educators to teach while loops before for loops. I 
think that is a terrible idea, while loops are so much more complicated than 
for loops.

Try this example instead:

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
if i == 2:
print("skipping")
continue
if i == 5:
print("breaking")
break
print("i =", i)


The main thing you need to know to understand this is that the line "for i in 
..." sets i=0, then runs the indented block under it, then sets i=1 and runs 
the indented block, then sets i=2, and so forth. Run the code and see if it 
makes sense to you. Feel free to ask as many questions as you like!


The line "for i in [0, 1, 2, ..." is a bit wordy and verbose. Can you imagine 
if you wanted to loop 1000 times? Fortunately Python has an abbreviated 
version:

for i in range(10):
if i == 2:
print("skipping")
continue
if i == 5:
print("breaking")
break
print("i =", i)



-- 
Steve

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-22 Thread Rustom Mody
On Thursday, June 23, 2016 at 9:47:23 AM UTC+5:30, Elizabeth Weiss wrote:
> CODE #1:
> 
> i=0
> while 1==1:
>print(i)
>i=i+1
>if i>=5:
>  print("Breaking")
>  break
> 
> --
> I understand that i=0 and i will only be printed if 1=1
> The results of this is
> 0
> 1
> 2
> 3
> 4
> Breaking
> 
> Why is Breaking going to be printed if i only goes up to 4? It does say if 
> i>=5? Shouldn't this mean that the results should be:
> 0
> 1
> 2
> 3
> 4
> 5
> 
> CODE #2:
> 
> i=0
> while True:
>i=i+1
>   if i==2:
>  print("Skipping 2")
>  continue
>   if i==5:
>  print("Breaking")
>  break
>print(i)
> 
> --
> 
> Questions:
> 1. what does the word True have to do with anything here? 
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> 4. How is i equal to 2 or 5 if i=0?
> 
> Thanks for all of your help!

I suggested in your other post (Subject While Loops)
that the predecessor of python ABC's syntax for assignment would help unconfuse 
you

ie the ASSIGNMENT x=y we write as PUT y IN x

Using that rewrite your CODE 1 as

PUT 0 in i
while 1==1:
   print(i)
   PUT i+1 IN i
   if i>=5:
 print("Breaking")
 break 

Now try and rethink what that does
Then repeat for your other examples that confuse
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Break and Continue: While Loops

2016-06-22 Thread Lawrence D’Oliveiro
On Thursday, June 23, 2016 at 4:17:23 PM UTC+12, Elizabeth Weiss wrote:
> 
> i=0
> while 1==1:
>print(i)
>i=i+1
>if i>=5:
>  print("Breaking")
>  break
> 
> Why is Breaking going to be printed if i only goes up to 4? It does say if
> i>=5?

Because you incremented i after printing its value, and before checking it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Break and Continue: While Loops

2016-06-22 Thread Elizabeth Weiss
CODE #1:

i=0
while 1==1:
   print(i)
   i=i+1
   if i>=5:
 print("Breaking")
 break

--
I understand that i=0 and i will only be printed if 1=1
The results of this is
0
1
2
3
4
Breaking

Why is Breaking going to be printed if i only goes up to 4? It does say if 
i>=5? Shouldn't this mean that the results should be:
0
1
2
3
4
5

CODE #2:

i=0
while True:
   i=i+1
  if i==2:
 print("Skipping 2")
 continue
  if i==5:
 print("Breaking")
 break
   print(i)

--

Questions:
1. what does the word True have to do with anything here? 
2. i=i+1- I never understand this. Why isn't it i=i+2?
3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
4. How is i equal to 2 or 5 if i=0?

Thanks for all of your help!

-- 
https://mail.python.org/mailman/listinfo/python-list


I donä't get while-loops

2008-08-02 Thread ssecorp
in read2 it never quits when I write quit, why?

def read():
expr = raw_input(Lisp )
if expr != quit:
print parse(expr)
read()
else:
print Good session!

def read2():
expr = 
while expr != quit:
expr = raw_input(Lisp )
print parse(expr)
read2()
print Good session!
--
http://mail.python.org/mailman/listinfo/python-list


Re: I donä't get while-loops

2008-08-02 Thread Stefaan Himpe


def read2():
expr = 
while expr != quit:
expr = raw_input(Lisp )
print parse(expr)
read2()

^

print Good session!



You shouldn't call read2() inside read2()...
just remove that line and retry...

Each time you call read2() recursively, a
new expr is initialized to , so the condition
never becomes true
--
http://mail.python.org/mailman/listinfo/python-list


Re: I donä't get while-loops

2008-08-02 Thread Larry Bates

ssecorp wrote:

in read2 it never quits when I write quit, why?

def read():
expr = raw_input(Lisp )
if expr != quit:
print parse(expr)
read()
else:
print Good session!

def read2():
expr = 
while expr != quit:
expr = raw_input(Lisp )
print parse(expr)
read2()
print Good session!


That's because read2() is being called recursively making expr be always a blank 
string the first time you go through the loop.  Delete the call to read2() that 
is inside your while loop and try.


-Larry
--
http://mail.python.org/mailman/listinfo/python-list


Re: I donä't get while-loops

2008-08-02 Thread ssecorp
oops, embarrassing, I created the while loop not to use recursion then
I still did...
--
http://mail.python.org/mailman/listinfo/python-list


Re: I donä't get while-loops

2008-08-02 Thread Tyler Breisacher
You're actually calling the read2() function from within read2(). This 
is called recursion, and it is *not* what you want in this case, since 
it unnecessarily fills up your call stack. Remember that a while loop 
automatically goes back to the top without you having to re-call your 
function. I would just do this:


def read3():
expr = raw_input(Lisp )
while expr != quit:
print parse(expr)
expr = raw_input(Lisp )
print Good session!


ssecorp wrote:

in read2 it never quits when I write quit, why?

def read():
expr = raw_input(Lisp )
if expr != quit:
print parse(expr)
read()
else:
print Good session!

def read2():
expr = 
while expr != quit:
expr = raw_input(Lisp )
print parse(expr)
read2()
print Good session!



--
http://mail.python.org/mailman/listinfo/python-list


[issue2688] Error when nesting many while loops

2008-08-01 Thread Jack Diederich

Jack Diederich [EMAIL PROTECTED] added the comment:

This was fixed in more recent versions of 2.5, it now raises a
SystemError: too many statically nested blocks

Thanks for the tip Guilherme.

Marking closed.

--
nosy: +jackdied
resolution:  - out of date
status: open - closed

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2688
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2688] Error when nesting many while loops

2008-04-25 Thread Guilherme Polo

Guilherme Polo [EMAIL PROTECTED] added the comment:

Apparently this was fixed at rev 52504 python-trunk. 
Which python version did you use ?

--
nosy: +gpolo

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2688
__
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2688] Error when nesting many while loops

2008-04-25 Thread Robert Greimel

New submission from Robert Greimel [EMAIL PROTECTED]:

nesting 20 while loops works OK.
The 21st leads to the cryptic error message

python: Python/compile.c:295: PyAST_Compile: Assertion `co ||
PyErr_Occurred()' failed.
Abort

when trying to execute the script.

--
components: None
messages: 65778
nosy: rgreimel
severity: normal
status: open
title: Error when nesting many while loops
type: compile error
versions: Python 2.5

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2688
__
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2688] Error when nesting many while loops

2008-04-25 Thread Robert Greimel

Robert Greimel [EMAIL PROTECTED] added the comment:

Python 2.5 (r25:51908, Nov  6 2007, 15:55:44) 

from Fedora 7 (python-2.5-15.fc7)

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2688
__
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



for and while loops

2006-06-28 Thread [EMAIL PROTECTED]
i was wondering if anyone could point me to some good reading about the
for and while loops

i am trying to write some programs
Exercise 1

Write a program that continually reads in numbers from the user and
adds them together until the sum reaches 100. Write another program
that reads 100 numbers from the user and prints out the sum. 

but im not quite grasping those functions..

please bear im mind i am an extreme newbie at this...thanks in advance

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for and while loops

2006-06-28 Thread Bayazee
hi

#Exercise 1 :
s=0
while 1:
  s+=input(Enter a num : )
  if s=100:
print The sum is greater than 100 : ,s
break

#Exercise 1 :
s=0
for i in range(5):
  s+=input(Enter num #%d  %(i+1))
print The Sum is :  , s

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for and while loops

2006-06-28 Thread Bayazee
hi

#Exercise 1 :
s=0
while 1:
  s+=input(Enter a num : )
  if s=100:
print The sum is greater than 100 : ,s
break

#Exercise 2 :
s=0
for i in range(5):
  s+=input(Enter num #%d  %(i+1))
print The Sum is :  , s

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for and while loops

2006-06-28 Thread Schüle Daniel
[EMAIL PROTECTED] schrieb:
 i was wondering if anyone could point me to some good reading about the
 for and while loops
 
 i am trying to write some programs
 Exercise 1
 
 Write a program that continually reads in numbers from the user and
 adds them together until the sum reaches 100. Write another program

the hidden hint here is ... read until
you can't know ahead how many numbers it will be, the pattern in this 
case is to use while sum smaller then 100
sum = 0
while sum  100:
 sum = sum + input(more numbers please: )


 that reads 100 numbers from the user and prints out the sum. 

here you know that you are going to read exactly 100 numbers
sum = 0
for i in range(100):
 sum = sum + input(please number #%i:  % (i+1))


the only unclear point here is range(100)
it generates a list with number [0,1,2 ... 99]
and iterates through it
one could write it like
for i in [0,1,2,3,4]:
 do_something_with(i)

but it gets tedious to write such a long list

 
 but im not quite grasping those functions..
 
 please bear im mind i am an extreme newbie at this...thanks in advance
 

hth, Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for and while loops

2006-06-28 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
 i was wondering if anyone could point me to some good reading about the
 for and while loops

There's not much to say.

while expr:
   block

will execute block as long as expr is True.

for item in sequence:
   block

will execute block for each item in sequence.

ie :
for letter in [a, b, c]:
   do_something_with(letter)

is equivalent to

letter = a
do_something_with(letter)
letter = b
do_something_with(letter)
letter = c
do_something_with(letter)


 i am trying to write some programs
 Exercise 1
 
 Write a program that continually reads in numbers from the user and
 adds them together until the sum reaches 100.

Since it's nearly impossible to predict how much iteration will be 
necessary for this condition to be satisfied[1], you want a while loop. 
The condition is 'the_sum = 100' (starting with 'the_sum == 0'). The 
body of the loop is mainly : read a number in, add it to the_sum.

[1] FWIW, we have 0  number of iterations  +infinity, since nothing 
specifies that the user can not enter negative numbers !-)

 Write another program
 that reads 100 numbers from the user and prints out the sum. 

Here you have a definite number of iterations, so it's a clear use case 
for a for loop, which will take care of the loop count by itself. Now 
since the for loop iterates over a sequence, you need such a sequence of 
100 items. The canonical solution is the range(count) function, which 
will produce a sequence of count integers. The body of the loop is 
exactly the same as in the previous case.

 but im not quite grasping those functions..

which 'functions' ? 'for .. in ..' and 'while ..' are statements 
(instructions), not functions. A functions is eval'd, and returns a 
value. A statement is executed, and has no value.

HTH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for and while loops

2006-06-28 Thread Simon Forman
[EMAIL PROTECTED] wrote:
 i was wondering if anyone could point me to some good reading about the
 for and while loops

 i am trying to write some programs
 Exercise 1

 Write a program that continually reads in numbers from the user and
 adds them together until the sum reaches 100. Write another program
 that reads 100 numbers from the user and prints out the sum. 

 but im not quite grasping those functions..

 please bear im mind i am an extreme newbie at this...thanks in advance

while loops test a condition every time through the loop and keep
running as long as it's true.

for loops run through a finite (usually) set of things and do something
for each thing.


Check out one or more python tutorials such as
http://docs.python.org/tut/tut.html (and especially this part
http://docs.python.org/tut/node6.html),  a quick search an google can
turn up more.

(Hint:  you're probably gonna want to use a while loop for the first
program and a for loop for the second.)

Hope that helps,
~Simon

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for and while loops

2006-06-28 Thread Bruno Desthuilliers
Bayazee a écrit :
 hi
 
 #Exercise 1 :
 s=0
 while 1:
   s+=input(Enter a num : )
   if s=100:
 print The sum is greater than 100 : ,s
 break

Why do you manually check the condition when the construct is meant to 
take care of it ?

the_sum = 0
while the_sum  100:
   try:
 sum += int(raw_input(please enter a num: )
   except ValueError:
 pass


 #Exercise 1 :
 s=0
 for i in range(5):

Was supposed to be 100, not 5.

   s+=input(Enter num #%d  %(i+1))

idem as above. Using input() is usually a bad idea:

  import sys
  s = input(please hack my machine: )
please hack my machine: sys.path.insert(0, '/path/to/malicious/stuff')
  sys.path[0]
'/path/to/malicious/stuff'
 

-- 
http://mail.python.org/mailman/listinfo/python-list