Require code that starts within the middle of the for loop ranges.


Joined
Apr 7, 2022
Messages
9
Reaction score
0
Hi guys,

I require code that starts within that starts within the middle of the for loop ranges, and then continues to the end of the for loop range.


Code:
#here is my attempt of the code:
Yi01 = 2
Xi01 = 3
for Yi01 in range(1,4):
    for Xi01 in range(1,5):
        print(str(Yi01) + " " + str(Xi01))
#The above code does not print what I need

Below is what I require to be printed
#2 3
#2 4
#2 5
#3 1
#3 2
#3 3
#3 4
#3 5
#4 1
#4 2
#4 3
#4 4
#4 5

Any ideas?
 
Ad

Advertisements

Joined
Apr 7, 2022
Messages
9
Reaction score
0
Hi guys,

just came up with a solution (kinda).

Just use the first for loop to complete the last bit of the X.
and then use a second set of "for" loops to continue from Y+1 and X=1.

see below:

Code:
for Xi01 in range(Xi01,5):
    print(str(Yi01) + " " + str(Xi01))

for Yi01 in range(Yi01+1,4):
    for Xi01 in range(1,5):
        print(str(Yi01) + " " + str(Xi01))
#code is untested but hopefully you get the idea.

However, if anyone has a clearer solution, I'd love to hear it. Also this solution would become much harder if there were 3 or more variables instead of 2.
 
Joined
Apr 7, 2022
Messages
9
Reaction score
0
Actually,

My question is still valid.

I am wondering how I can have code inside my loops that change the "range" and current Yi01 or Xi01 value to a different number.

e.g given:
Code:
for Yi01 in range(1,4):
    for Xi01 in range(1,5):

I would like code that would jump Xi01 or Yi01 to a particular value and then keep the loops running from that new Xi01 or Yi01 values.
 
Ad

Advertisements

Joined
Mar 28, 2022
Messages
71
Reaction score
8
Does the statement "for x in range(3,5)" make sense in Python?

If it doesn't then subtract what it takes to get the first number down to 1, from both, and then add it back before displaying.
3-1=2; so range (3-2, 5-2)
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top