Reputation: 31
My initial code...
name="LOVE"
i=0
while i < len(name):
i=i+1
if name[i] == "V":
print('Hi')
continue
print('Hello')#never printed
print(name[i])
print("The end")
The desired outcome is
L
O
Hi
E
The end
I have read other answers on this topic and I was either unable to understand or didn't find the answer I was looking for. I know writing continue in while loop is bad practice, even so I wanted to know whether there was a way to use it without throwing an error(logical or IndexError) or going to an infinite loop. 'while' as of my knowledge, doesn't have an option of
while(i++<len(name))
in python, so that didn't work. Since the control is transferred to the beginning of the loop and test expression is not evaluated again, I tried keeping the increment statement above the continue block and the print statement below to be logically consistent with the desired output. First I lost 'L' in my output with 'IndexError' at 'if' statement and I was expecting the same error at print statement in the end. So I tried to improve by beginning index from -1 instead and catching my error.
name="LOVE"
i=-1
try:
while i < len(name):
i=i+1
if name[i] == "V":
print('Hi')
continue
print('Hello')#never printed
print(name[i])
except IndexError:
print()
print("The end")
In using for, the continue statement transfers control simply to next iteration in which by virtue of being the next iteration the counter(val) is already incremented so it works fluidly. Like below,
for val in "LOVE":
if val == "V":
print('Hi')
continue
print('Hello')#never printed
print(val)
print("The end")
Again I know of this but that is not what I am interested in... In summary what I would like to know is if there is a more elegant way to do this without a try catch block, some other kind of implementation of continue in while to reach desired output in Python 3.x? P.S:-this is my first question on this forum, so please go a bit easy on me.
Upvotes: 2
Views: 351
Reputation: 1
string="LOVE"
i=0
while i<len(string):
if string[i]=="V":
print("HI")
i+=1
continue
else:print(string[i])
i+=1
print("Thank You")
Upvotes: 0
Reputation: 11
The elegant (pythonic) way to solve this problem is to use for-loop, like others have suggested. However, if you insist on using while-loop with continue, you should try to tweak the condition of the while-loop. For example like this:
name="LOVE"
i=-1
while i < len(name)-1:
i=i+1
if name[i] == "V":
print('Hi')
continue
print('Hello')#never printed
print(name[i])
print("The end")
Upvotes: 1
Reputation: 15887
This really is a for
job. You want an end to the looping, represented by both the except
clause and the i < len(name)
condition, and a loop increment, presented by the i=i+1
increment. Both of these tasks are typically performed by an iterator, and strings are iterable. It is possible to rewrite a for
in while
form, but it's not terribly meaningful:
i = iter(name)
while True:
try:
c = next(i)
except StopIteration:
break
print('Hi' if c == 'V' else c)
It's just much easier to write for c in name
.
You could also use a finally
, which should execute no matter how the try
is left:
i = 0
while i < len(name):
try:
if ...:
continue
finally:
i += 1
Upvotes: 1
Reputation: 532238
You don't really need continue
, just an else
clause on your if
statement. (Also, since i == 0
to start, you need to wait until you've used i
as an index before incrementing it.)
name="LOVE"
i=0
while i < len(name):
if name[i] == "V":
print('Hi')
else:
print(name[i])
i=i+1
print("The end")
or
for c in name:
if c == "V":
print('Hi')
else:
print(c)
Upvotes: 2