Home > database >  Why i value in for loop didn't go normal?
Why i value in for loop didn't go normal?

Time:10-04

for i in range(2,5):
    for j in range(2,i):
        print(f"i:{i}\tj:{j}")

enter image description here

I don't know why the output of i vaule isn't start with 2, anyone could explain this to me?

CodePudding user response:

It's because in the range function, it doesn't include the last number so range(2,2) doesn't do anything because it doesn't include the 2, and for example range(2, 3) would only include the 2 because it wouldn't include the 3.

CodePudding user response:

The value of i starts with 2. It is just that range(2,2) isn’t going to execute.

for i in range(2,5):
   for j in range(2,i 1):
      print(f"i:{i}\tj:{j}")

Change i into i 1 in the nested loop.

  • Related