Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While & For Loops
#1
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..
Reply
#2
indent your print, it's not part of the inner for loop

the while will make this run forever, remove it
Reply
#3
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 code

for <>:
    if i == 3:
        continue
    <long
    block 
    of 
    code
    saved
    from
    indentation>
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

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