Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to update a variable
#1
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 ?
Reply
#2
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.
Reply
#3
how do i get line 7 to be in the inner loop ?

thanks for the response !
Reply
#4
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).
Reply
#5
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) ?
Reply
#6
second = 1
third = 1
while second < 100:
print(second)
second += 1
Reply
#7
hello newerpython,

the code you wrote doesn't work as well,
and it does not include the nested loop...

thank you in anyway...
Reply
#8
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)
Reply
#9
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 ?
Reply
#10
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,711 Dec-16-2020, 05:26 AM
Last Post: Vokofe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020