python - I need the print statement to stay in the If-loop so it is conditional, but out of the for loop so it doesn't repeat a ton of times -


im using loop. iterates through bunch of commands , @ end, want print something. within loop, if-else statement.

i use break break out if- statement, goes straight else portion.

for x in list:    if x 1:       bunch of commands       break    else:       bunch of other commands print 'success' 

i need print statement stay in if-loop conditional, out of loop doesn't repeat ton of times. ideas?

i want print 'success' when when x not equal 1. once @ end.

you can use flag -- variable set indicate event occurred (in case, else branch reached).

success = false x in list:    if x 1:       bunch of commands       break    else:       bunch of other commands       success = true  if success:     print 'success' 

what's happening here else case may reached multiple times in loop, setting success variable true (potentially) multiple times. if statement @ end checks if flag true @ end, 'success' printed @ once.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -