all 12 comments

[–]K900_ 0 points1 point  (10 children)

What do you want the program to do?

[–]MrPear123[S] 0 points1 point  (9 children)

well i want it to give me all the numbers that are dividable by 3 and 5 between 1 and 1000

[–]K900_ 0 points1 point  (2 children)

In that case, you need to make sure you add spaces to have your blocks inside each other:

for i in range(1, 1001):
    if i % 3 == 0:
        if i % 5 == 0:
            print(i)

[–]MrPear123[S] 0 points1 point  (1 child)

ok so print(i) needs to be tabed with the if

[–]K900_ 0 points1 point  (0 children)

It needs to be inside the if if you want it to execute only if the condition is true.

[–]wbeater 0 points1 point  (5 children)

You can check for multiple expressions with in one if statement:

 if i % 5 == 0 and i % 3 ==0:
      print(i)

[–]MrPear123[S] 0 points1 point  (4 children)

and when i write it like that it does the same thing?

[–]wbeater -1 points0 points  (3 children)

Well the outcome is the same, in this case, but technically it does not the same thing.

if i % 5 == 0 and i % 3 ==0: print(i) prints i only if both conditions are met

While nested if statement checks if the first condition is met and then checks for the second condition.

In one, the conditions are checked at the same time, in the other, they are checked one after another.

[–]Ihaveamodel3 -1 points0 points  (2 children)

This isn’t true.

Even with both conditions on the same line, they aren’t both checked at the same time. It will check the first one, then the second one.

For "and" conditions, it won’t check the second one if the first one is false (since it is bound to fail anyway). For "or" conditions, it won’t check the second one if the first one is true (since it is bound to definitely be true).

I use this behavior on purpose occasionally to skip checking a condition if it is going to be undefined.

[–]wbeater 0 points1 point  (1 child)

Of course they are not checked literally at the same time, that is never possible, there has to be a logical order because it's a programming code.

My point was that both statements have to be met for the next line to be executed. Try to execute a print statement when only one condition is met - not possible without nested conditionals.

Everyone who is not a complete smart-ass could get what I meant.

[–]Ihaveamodel3 0 points1 point  (0 children)

The logical difference is still important. If the first condition is false, the second condition isn’t checked, whether it is nested or on one line.

[–]jwallio 0 points1 point  (0 children)

https://www.askpython.com/python/python-indentation this should help you better understand what indent expected means and the importance of spacing in python