Reputation:
count = 0
while count ** 2 < snum_:
print "Using search with increment 1, the root lies between", count,"and", count + 1
count = count + 1
How do I get the loop to only print the last possible line?
Upvotes: 1
Views: 2716
Reputation: 311428
You could save the string you want to print and print it after the loop:
count = 0
result = ''
while count ** 2 < snum_:
result = "Using search with increment 1, the root lies between %d and %d" % count, count + 1
count = count + 1
print result
Upvotes: 0
Reputation: 5696
You can try this:
count = 0
while count < 4:
print('hi')
count += 1
else:
# Replace below string with what you wish.
print('end')
+=
means count + 1
. else
is reached after while
finishes (will not print, if you break
the loop instead of letting it finish normally).
Upvotes: 0
Reputation: 27283
With a for-loop and itertools.count()
:
import itertools
for count in itertools.count(1):
if count**2 >= snum_:
print "Using search with increment 1, the root lies between %d and %d" % count-1, count
break
But the general idea can also be applied to your while loop: When count**2
is no longer less than snum_
, print and break.
Upvotes: 0