Posts: 230
Threads: 39
Joined: Mar 2020
here is the code:
second = 1
third = 1
while second < 100:
while third < 50:
print(third)
third += 1
print(second)
second += 1 why does it print only:
1
1
1
1
1
and so on, without increasing ?
Posts: 1,583
Threads: 3
Joined: Mar 2020
Apr-06-2020, 08:46 AM
(This post was last modified: Apr-06-2020, 08:47 AM by bowlofred.)
Your innermost loop is lines 5 and 6.
while third < 50:
print(third) There are no instructions here to modify third (or to break out of the loop), so it prints the same value continuously.
Line 7 is part of the outer loop, not the inner loop.
Posts: 230
Threads: 39
Joined: Mar 2020
how do i get line 7 to be in the inner loop ?
thanks for the response !
Posts: 1,583
Threads: 3
Joined: Mar 2020
The indentation controls that. As line 7 goes back to the previous level of indentation, it marks itself as outside the previous scope.
You're only using one space for your indentation. While legal, it makes it much harder to see which statement is at what level. The recommendation is to use four spaces, which makes the statement levels more easily visible.
second = 1
third = 1
while second < 100:
while third < 50:
print(third)
third += 1
print(second)
second += 1 Lines 8 and 9 are outside all loops and would only be executed if the loop were ended (which it will not with the current code).
Posts: 230
Threads: 39
Joined: Mar 2020
Apr-06-2020, 03:54 PM
(This post was last modified: Apr-06-2020, 03:54 PM by astral_travel.)
hi bowlofred,
can you please write here the correct code so i can simply see how to fix it (or anyone else for that matter) ?
Posts: 1
Threads: 0
Joined: Apr 2020
second = 1
third = 1
while second < 100:
print(second)
second += 1
Posts: 230
Threads: 39
Joined: Mar 2020
hello newerpython,
the code you wrote doesn't work as well,
and it does not include the nested loop...
thank you in anyway...
Posts: 7,319
Threads: 123
Joined: Sep 2016
Apr-06-2020, 05:56 PM
(This post was last modified: Apr-06-2020, 05:56 PM by snippsat.)
The reason why your first code don't work is indentation as mention bye @ bowlofred.
Indentation in python is very important and use 4-space(always),then what's in blocks/scope get clearer.
Here with a marker,so while loop third will run all before second get executed.
second = 1
third = 1
while second < 100:
while third < 50:
third += 1
print(f'{third} **')
second += 1
print(second)
Posts: 230
Threads: 39
Joined: Mar 2020
hi snippsat,
i tried to run the fixed code you wrote, but it gave me an error, as follows:
Error: File "secondthird.py", line 6
print(f'{third} **')
^
SyntaxError: invalid syntax
btw, isn't possible to update the variable after the print() function ?
Posts: 7,319
Threads: 123
Joined: Sep 2016
Apr-06-2020, 07:46 PM
(This post was last modified: Apr-06-2020, 07:48 PM by snippsat.)
You should update your Python to 3.6 or newer, Python 3.8 (3.6-3.7) and pip installation under Windows
I use f-string here,which was great update in 3.6(4-year ago).
Here the same with older format() .
second = 1
third = 1
while second < 100:
while third < 50:
third += 1
print('{} **'.format(third))
second += 1
print(second)
|