2

I am new in python and I don't know why this program is not updating the 'i' variable when I am incrementing it in the execution of for loop. len(a) = 10 you can see in the code that I am incrementing 'i' time after time but after the increment when I comes into loop iteration it nullifies the update made in the body of loop. Why is this is ? It should be updated in general and loop execution should be less than 10. Please help.

final_result = 0
a= '3 4  4 5 6'
for i in range(0,len(a)):
    print('iteration')
    print('i is = ')      
    print(i)
    if a[i] is ' ' and a[i+1] is not ' ':
        if i-1 is 0:
            final_result = int(a[i-1]) + int(a[i+1])
            i += 2                           //here goes the increment
            print('1a- m here')
            print(final_result)
            print('i is = ')
            print(i)
        else:
            final_result = final_result + int(a[i+1])
            i += 2                              //here goes the increment
            print('1b- m here')
            print(final_result)
    elif a[i] is ' ' and a[i+1] is ' ':
        if i-1 is 0: 
            final_result = int(a[i-1]) - int(a[i+1])
            i += 3                           //here goes the increment
            print('2a- m here')
            print(final_result)
        else:
            final_result = final_result - int(a[i+2])
            i += 3                                 //here goes the increment
            print('2b- m here')
            print(final_result)
            print('i is = ')
            print(i)
print(final_result)
3
  • 3
    Because in the next iteration i takes up the next value from the list returned by the range Commented Jun 4, 2015 at 10:18
  • 2
    You should be using while loops for these purposes as the increment are conditional Commented Jun 4, 2015 at 10:19
  • What are you trying to do? There is almost certainly a better way to do it. Commented Jun 4, 2015 at 11:06

1 Answer 1

1

So several things:

  • Use a while loop.
  • Initialize i = 0
  • Increment i += 1 when none of your conditions match
  • Comments in Python are written with # comment, not // comment.

Example:

final_result = 0
a = '3 4  4 5 6'
i = 0
while i < len(a):
    print('iteration')
    print('i is = ')
    print(i)
    if a[i] is ' ' and a[i + 1] is not ' ':
        if i - 1 is 0:
            final_result = int(a[i - 1]) + int(a[i + 1])
            i += 2  # here goes the increment
            print('1a- m here')
            print(final_result)
            print('i is = ')
            print(i)
        else:
            final_result = final_result + int(a[i + 1])
            i += 2  # here goes the increment
            print('1b- m here')
            print(final_result)
    elif a[i] is ' ' and a[i + 1] is ' ':
        if i - 1 is 0:
            final_result = int(a[i - 1]) - int(a[i + 1])
            i += 3  # here goes the increment
            print('2a- m here')
            print(final_result)
        else:
            final_result = final_result - int(a[i + 2])
            i += 3  # here goes the increment
            print('2b- m here')
            print(final_result)
            print('i is = ')
            print(i)
    else:
        i += 1
print(final_result)

Output:

$ python3.4 foo.py
iteration
i is = 
0
iteration
i is = 
1
1a- m here
7
i is = 
3
iteration
i is = 
3
2b- m here
3
i is = 
6
iteration
i is = 
6
1b- m here
8
iteration
i is = 
8
1b- m here
14
14
Sign up to request clarification or add additional context in comments.

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.