2

I just want to know the logic within this problem. I just want my program to execute another loops once the first loop was done.

for i in range(1,5):
    for j in range(1,5):
        print(i*j,end="\t")
    print("\n")

After the code above was done I want my program to perform another loop which is this one:

for i in range(5,1,-1):
    for j in range(5,1,-1):
        print(i*j,end="\t")
    print("\n")

1
  • 2
    Not entirely sure what you're asking. Are you saying you want to complete your first code sample, and then immediately do the next one? Because if that's the case then why not just put the second block of code immediately after the first one in your .py file? Again, I may be misunderstanding what you asked Commented Apr 11, 2020 at 6:06

1 Answer 1

1

Just order them how you want them executed (in this case, one after the other). Python, among many other languages will just execute code sequentially unless you specifically introduce control structures like loops:

for i in range(1,5):
    for j in range(1,5):
        print(i*j,end="\t")
    print("\n")
for i in range(5,1,-1):
    for j in range(5,1,-1):
        print(i*j,end="\t")
    print("\n")

Just be aware that the second loop isn't a perfect reversal of the first. Since ranges are inclusive at the start and exclusive at the end, the tow complementary loops would be:

for i in range(1,5):        # 1..4 inclusive
for i in range(4,0,-1):     # 4..1 inclusive
Sign up to request clarification or add additional context in comments.

3 Comments

Infinity, it was your bizarre claim that being interpreted somehow gave Python the ability to execute code sequentially. Since just about every procedural language (compiled or not) has this ability (and since Python (CPython) is compiled, just on demand, and not to machine language), this just isn't true. I'll be happy to change that to an upvote if that erroneous information is removed, since your answer is otherwise okay.
Actually, I'll just make a suggested change and cancel the downvote. You can accept, modify or roll back as you see fit.
Ok. Of course I respect you changes and comments. I'm just 16, trying to gain up in Python by researching on doubts. Thank you

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.