python - Replace iterated output with custom print statement -
def the(x): = 0 while < 6: += x print "i equals %s" % if == 5: print "this replaces 5th iteration" i have loop increments 1, stops @ < 6 , prints string every iteration.
i want remove 5th iteration ("i equals 5") , replace string: "this replaces 5th iteration".
what options?
check condition before printing using else statement. have prints 5th 1 before checking if it's 5th one. (i added parentheses print because use python 3, should still work in python 2)
def the(x): = 0 while < 6: += x if == 5: print("this replaces 5th iteration") else: print("i equals %s" % i) >>> the(1) equals 1 equals 2 equals 3 equals 4 replaces 5th iteration equals 6
Comments
Post a Comment