while True: for i in range(1,5): if i==3: continue print("{:>2}SAM".format(i))For this piece of code, why does it infinitely print out only 4SAM instead of infinitely doing:
1SAM
2SAM
3SAM
4SAM
and so on..
While & For Loops
|
while True: for i in range(1,5): if i==3: continue print("{:>2}SAM".format(i))For this piece of code, why does it infinitely print out only 4SAM instead of infinitely doing: 1SAM 2SAM 3SAM 4SAM and so on..
indent your print, it's not part of the inner for loop
the while will make this run forever, remove it
Jun-22-2018, 09:14 AM
if i != 3: print(...)makes more sense. continue should be avoided - unless you check the condition close to the beginning of the cycle, and you want to avoid indenting large block of codefor <>: if i == 3: continue <long block of code saved from indentation>
Test everything in a Python shell (iPython, Azure Notebook, etc.)
|
|