0

The i+=1 isn't working, it should have increased the i value but it isn't.

n = int(input())

for j in range(n):
    a = input()
    pair = 0
    
    for i in range(len(a)-1):
        print(i)
        if a[i] == "x" and a[i+1] == "y":
            pair += 1
            print("*")
        elif a[i] == "y" and a[i+1] =="x":
            pair += 1
            print('**')
        else:
            continue
        i+=1
        print(pair)
        print("****")
    print(pair)

3 Answers 3

1

You are trying to modify a parameter that is implicitly set by the for loop. This isn't C-Code, increasing the counter variable will not skip the next iteration.

The reason for that is quite simple: for itself does not increase i in every iteration, it just steps through the given iteratable. In this case the iteratable is a range, which behaves like for would increase i every iteration, but it really just takes the next value from the range.

So i+=1 doesn't have an effect on the next iteration, as it doesn't modify the next value in the range.

Sign up to request clarification or add additional context in comments.

Comments

0

Your for-loop already increments i on every iteration. But if you want to increment by more than 1 when certain condition is satisfied, then you can use while loop as follows:

n = int(input())

for j in range(n):
    a = input()
    pair = 0
    i = 0
    while i < len(a)-1:
        print(i)
        if a[i] == "x" and a[i+1] == "y":
            pair += 1
            print("*")
        elif a[i] == "y" and a[i+1] =="x":
            pair += 1
            print('**')
        else:
            i += 1
            continue
        i+=2
        print(pair)
        print("****")

    print(pair)strong text

3 Comments

This requires having a i += 1 on every continue, which is very error prone imo. I'd suggest wrapping the i += 1 with a try: finally.
Sorry, I don't really see how it could go wrong. continue just says move on to the next iteration, which starts with checking if i < len(a) - 1. This upper bound makes sure that i and i+1 are valid indices of a,
Of course your code is correct, but somebody when editing it later will forget the i += 1 in some special case. With robust i mean robust in future editing.
0

You could explicitely skip the next iteration when a pair is found.

Here is your code with the changes pointed at by # <---.

n = int(input())

for j in range(n):

    a = input()
    pair = 0
    skip_next = False # <---
    for i in range(len(a)-1):
        if skip_next is True: # <---
            skip_next = False # <---
            continue          # <---
        print(i)
        if a[i] == "x" and a[i+1] == "y":
            pair += 1
            print("*")
            skip_next = True # <---
        elif a[i] == "y" and a[i+1] =="x":
            pair += 1
            print('**')
            skip_next = True # <---
        else:
            continue
        print(pair)
        print("****")
    print(pair)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.