Python Forum
Change the varaible in For Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change the varaible in For Loop
#1
Hello,
I have a very simple problem:
I am trying to increase the i in for loop like that:
1 2
4 5
3 4
6 7
5 6
8 9
One step I am increasing the i with +3 and the other step I am decreasing the i with -1
for i in range(1,number_of_qubit-2):
       print(i,i+1)
       i=i+3
       print(i,i+1)
       i=i-1
However I am getting this result:
1 2
4 5
2 3
5 6
3 4
6 7
4 5
7 8
Why am I seeing 2 and 3 instead of 4 and 5 ? How can I fix this problem
Reply
#2
Did you know that Python comes with built-in help?

Quote:Why am I seeing 2 and 3 instead of 4 and 5 ?


>>> help('for')
The "for" statement
*******************

The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:
/.../
The for-loop makes assignments to the variables(s) in the target list.
This overwrites all previous assignments to those variables including
those made in the suite of the for-loop:

   for i in range(10):
       print(i)
       i = 5             # this will not affect the for-loop
                         # because i will be overwritten with the next
                         # index in the range
How to overcome? Maybe something like this:

>>> for i, j in enumerate(range(1, 4)):
...     print(i+j, i+j+1)
...     print(i+j+3, i+j+4)
...
1 2
4 5
3 4
6 7
5 6
8 9
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
range() is a generator. i is a variable that you are using to reference the value returned by the generator. The generator doesn't know anything at all about i, and changing i has no effect on the generator. You don't even need i for the generator to work.
for _ in range(5):  # Look Ma, no index!
    print('Hello')
This prints 'Hello' five times.

A while loop does a comparison, so changing the index value does have ah effect. This should work.
i = 0
while i < number_of_qubit-2:
       print(i,i+1)
       i=i+3
       print(i,i+1)
       i=i-1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Change variable value during a while loop? penahuse 2 1,542 Nov-15-2020, 11:53 PM
Last Post: penahuse
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,000 Sep-18-2019, 09:43 AM
Last Post: Larz60+
  change to loop amirliker 3 1,406 Jun-06-2019, 01:08 PM
Last Post: heiner55
  Call a varaible from class in the parent class Clement_2000 1 1,374 May-09-2019, 11:14 PM
Last Post: michalmonday
  change array column values without loop khalidreemy 2 1,691 May-05-2019, 09:05 AM
Last Post: DeaD_EyE
  Code Change Help: Infinite Loop Error bindulam 2 1,281 Mar-10-2019, 11:15 PM
Last Post: hshivaraj
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 5,302 Aug-30-2017, 12:14 PM
Last Post: metulburr
  Timestamp doesn't change with each loop lwhistler 2 4,957 Aug-23-2017, 06:58 PM
Last Post: bowen73
  Loop thru textfile, change 3 variables for every line herbertioz 11 6,126 Nov-10-2016, 04:56 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020