how to decrement and increment loop range 'i' variable in the execution of loop in python -


i new in python , don't know why program not updating 'i' variable when incrementing in execution of loop. len(a) = 10 can see in code incrementing 'i' time after time after increment when comes loop iteration nullifies update made in body of loop. why ? should updated in general , loop execution should less 10. please help.

final_result = 0 a= '3 4  4 5 6' in range(0,len(a)):     print('iteration')     print('i = ')           print(i)     if a[i] ' ' , a[i+1] not ' ':         if i-1 0:             final_result = int(a[i-1]) + int(a[i+1])             += 2                           //here goes increment             print('1a- m here')             print(final_result)             print('i = ')             print(i)         else:             final_result = final_result + int(a[i+1])             += 2                              //here goes increment             print('1b- m here')             print(final_result)     elif a[i] ' ' , a[i+1] ' ':         if i-1 0:              final_result = int(a[i-1]) - int(a[i+1])             += 3                           //here goes increment             print('2a- m here')             print(final_result)         else:             final_result = final_result - int(a[i+2])             += 3                                 //here goes increment             print('2b- m here')             print(final_result)             print('i = ')             print(i) print(final_result) 

so several things:

  • use while loop.
  • initialize i = 0
  • increment i += 1 when none of conditions match
  • comments in python written # comment, not // comment.

example:

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

output:

$ python3.4 foo.py iteration =  0 iteration =  1 1a- m here 7 =  3 iteration =  3 2b- m here 3 =  6 iteration =  6 1b- m here 8 iteration =  8 1b- m here 14 14 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -