Why does my for loop only input one value, why does it not loop?

I have this code here:

i = 0
j = 4

for number in range(7):
if i < 6:
print("* " * i)
i += 1

elif i > 3:
    print("* " * j)
    j = j - 1

print(number)

Why does the elif statement not loop? Can someone help me here

Hello @jaivalpatel334243915 and welcome to the Codecademy Forums!

Please format your code according to this article. Remember than elif statements are only executed if the preceding if statement’s condition is not satisfied. You can use another if statement if you want both code blocks to be evaluated.

More...

You can use j -= 1 rather than j = j - 1. They accomplish the same thing.

2 Likes

I tried both ways but the output returns:

**




6

i = 0
j = 4

for number in range(7):
    if i < 6:
        print("* " * i)
        i += 1 
  
    if i > 6: 
        print("* " * j)
        j = j - 1


print(number)

Why does it not print the second if?

Remember that when using range, you start from 0 and go to, in this case 7 - 1, meaning that the for loop iterates a total of 6 times. Take a look at the value of i as you go through each line of code. Is i ever greater than 6?

2 Likes

Ohhhhhh Thank you Thank you ! Can you give me some tips on how to become better at for loops?

No problem! To get better, practice, practice, practice!

Here are a few resources you can use:

2 Likes

Thank you very much !

1 Like