Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110283 > unrolled thread
| Started by | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| First post | 2016-06-21 20:50 -0700 |
| Last post | 2016-06-22 16:03 +0000 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.python
while Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-21 20:50 -0700
Re: while Loops Wildman <best_lay@yahoo.com> - 2016-06-21 23:04 -0500
Re: while Loops Rustom Mody <rustompmody@gmail.com> - 2016-06-21 21:04 -0700
Re: while Loops Ben Finney <ben+python@benfinney.id.au> - 2016-06-22 14:02 +1000
Re: while Loops Michael Torrie <torriem@gmail.com> - 2016-06-21 22:11 -0600
Re: while Loops alister <alister.ware@ntlworld.com> - 2016-06-22 08:37 +0000
Re: while Loops John Gordon <gordon@panix.com> - 2016-06-22 16:03 +0000
| From | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| Date | 2016-06-21 20:50 -0700 |
| Subject | while Loops |
| Message-ID | <0cf9a94e-b84c-460d-b03d-edf6a941adc0@googlegroups.com> |
i=1 while i<=5: print(i) i=i+1 The result is: 1 2 3 4 5 Why is one of the results 5 since i=i+1? Should the maximum result be 4 since 4 +1=5? Thanks for your help!
[toc] | [next] | [standalone]
| From | Wildman <best_lay@yahoo.com> |
|---|---|
| Date | 2016-06-21 23:04 -0500 |
| Message-ID | <wt2dnWFr04nak_fKnZ2dnUU7-NmdnZ2d@giganews.com> |
| In reply to | #110283 |
On Tue, 21 Jun 2016 20:50:24 -0700, Elizabeth Weiss wrote:
> i=1
> while i<=5:
> print(i)
> i=i+1
>
> The result is:
> 1
> 2
> 3
> 4
> 5
>
> Why is one of the results 5 since i=i+1? Should the maximum result be 4 since 4 +1=5?
>
> Thanks for your help!
The operator '<=' means less than or equal. To get the results
you are expecting, you need to use '<' only.
i=1
while i<5:
print(i)
i=i+1
1
2
3
4
--
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-06-21 21:04 -0700 |
| Message-ID | <9e389f1b-f5b7-4ee1-bb2d-1506e49d28b6@googlegroups.com> |
| In reply to | #110283 |
On Wednesday, June 22, 2016 at 9:20:35 AM UTC+5:30, Elizabeth Weiss wrote: > i=1 > while i<=5: > print(i) > i=i+1 > > The result is: > 1 > 2 > 3 > 4 > 5 > > Why is one of the results 5 since i=i+1? Should the maximum result be 4 since 4 +1=5? > Not sure what your question is But I guess you are being tripped up by a pervasive and confusing pun which you are inadvertently(?) using in one line: a> i = i+1 b> 4+1 = 5 a> is an assignment statement IN the programming language python b> is a math statement ABOUT python a> in math is meaningless (unless i is ∞ or something) b> in programming is a syntax error If you feel you are confused thats good; most professional programmers are more confused and dont know it. The predecessor of python -- ABC -- wrote i = i+1 as PUT i+1 IN i Unfortunately python followed the mainstream and confusified it to i=i+1
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2016-06-22 14:02 +1000 |
| Message-ID | <mailman.25.1466568307.11516.python-list@python.org> |
| In reply to | #110283 |
Elizabeth Weiss <cake240@gmail.com> writes: > Why is one of the results 5 since i=i+1? What do you think ‘i = i + 1’ means? (Asking because your idea of what that means may be affecting how you expect the loop to behave.) > Should the maximum result be 4 since 4 +1=5? Try “thinking like the computer”: perform the steps yourself, reading each statement as though you are the computer. Keep a pad and pencil at hand, and make notes on what changes as you go through. See what you-as-computer actually emit onto the paper, and see whether that matches what the Python session produces. -- \ “The difference between a moral man and a man of honor is that | `\ the latter regrets a discreditable act, even when it has worked | _o__) and he has not been caught.” —Henry L. Mencken | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2016-06-21 22:11 -0600 |
| Message-ID | <mailman.26.1466568695.11516.python-list@python.org> |
| In reply to | #110283 |
On 06/21/2016 09:50 PM, Elizabeth Weiss wrote: > i=1 > while i<=5: > print(i) > i=i+1 > > The result is: > 1 > 2 > 3 > 4 > 5 > > Why is one of the results 5 since i=i+1? Should the maximum result be 4 since 4 +1=5? > > Thanks for your help! If you trace the execution through in your mind it should be come clear. While always checks the expression first before running the body of the loop. If, during the loop body, i was 4, it is then incremented to 5. Now the top of the while loop checks the expression and since i is 5, 5<=5 is true (5 is less than or equal to 5), so the body runs now, and prints out the value and then increments i by 6, making it 6. Now at the top of the while loop, 6 is clearly not less than or equal to 5, so the loop terminates there. Hope this helps.
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.ware@ntlworld.com> |
|---|---|
| Date | 2016-06-22 08:37 +0000 |
| Message-ID | <T6saz.267664$Rl4.55607@fx33.am4> |
| In reply to | #110283 |
On Tue, 21 Jun 2016 20:50:24 -0700, Elizabeth Weiss wrote: > i=1 while i<=5: > print(i) > i=i+1 > > The result is: > 1 > 2 > 3 > 4 > 5 > > Why is one of the results 5 since i=i+1? Should the maximum result be 4 > since 4 +1=5? > > Thanks for your help! check you loop condition while i lest than or equal to 5 so the loop will run for the case when i= 5 -- Welcome to Utah. If you think our liquor laws are funny, you should see our underwear!
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2016-06-22 16:03 +0000 |
| Message-ID | <nkecrm$40c$1@reader1.panix.com> |
| In reply to | #110283 |
In <0cf9a94e-b84c-460d-b03d-edf6a941adc0@googlegroups.com> Elizabeth Weiss <cake240@gmail.com> writes:
> Why is one of the results 5 since i=i+1? Should the maximum result
> be 4 since 4 +1=5?
"while i<=5" means "while i is less than or equal to 5". So the loop
will keep going at 5, and only stop when i is 6.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web