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
Post a Comment