Python Forum
Different Indentation Different Result
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Different Indentation Different Result
#1
sump=0
for num in listp:
    if num%2==0:
        sump=sump+num
        print(sump
sump=0
for num in listp:
    if num%2==0:
        sump=sump+num
    print(sump)
sump=0
for num in listp:
    if num%2==0:
        sump=sump+num
print(sump)
Here are technically the same code, but as I play around with the indentation of print(sump) Im having differente results.

Could someone explain to me why is it about the indentation of print that makes the result different every time?
Reply
#2
(Feb-11-2020, 09:01 PM)el_bueno Wrote: Here are technically the same code
well, technically it's not the same code due to different indentation.
The difference is self evident given the output. what is not clear?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Feb-11-2020, 09:01 PM)el_bueno Wrote: Here are technically the same code
They aren't the same. Each one tells it to print at different times.

The first one is after a for and if statements, the second is only in the for loop, and the last is global. The indentation is changing what needs to be the case in order to print sump.

Indentation is of paramount importance and changes everything. It is a huge factor in when and if a piece of code gets executed.
Reply
#4
(Feb-11-2020, 09:17 PM)buran Wrote:
(Feb-11-2020, 09:01 PM)el_bueno Wrote: Here are technically the same code
well, technically it's not the same code due to different indentation.
The difference is self evident given the output. what is not clear?

what is not clear is why I get different result with different indentation, I Want to understand the different results.
Reply
#5
Do you understand how if and for work and that the indentation determine which statements are part of those blocks?
Reply
#6
(Feb-12-2020, 07:35 PM)ndc85430 Wrote: Do you understand how if and for work and that the indentation determine which statements are part of those blocks?

I'm a complete beginner so It should help me if you would explain it to me. Pray
Reply
#7
Out of interest, how are you learning the language? If you don't have a decent book or material, I'd suggest that you should get one.
Reply
#8
(Feb-12-2020, 08:09 PM)el_bueno Wrote:
(Feb-12-2020, 07:35 PM)ndc85430 Wrote: Do you understand how if and for work and that the indentation determine which statements are part of those blocks?

I'm a complete beginner so It should help me if you would explain it to me. Pray

Read this page about indentation from w3schools
Reply
#9
(Feb-12-2020, 08:29 PM)ndc85430 Wrote: Out of interest, how are you learning the language? If you don't have a decent book or material, I'd suggest that you should get one.

Im on Udemy with a course Called Complete Python Bootcamp Go from zero to hero in Python 3, I would stick to the course, but I need sometimes to go further and ask myself questions like the one I'm asking here so I can go deeper to fully understand Python.

And I read some articles along the way before I post a question here.

And if you have any thing interesting about indentation I would much appreciate.
Reply
#10
(Feb-13-2020, 12:19 AM)el_bueno Wrote: And if you have any thing interesting about indentation I would much appreciate.

Indentation is really basic to how it all works. You won't find any special writing on it any more than a collage English book is going to review the order of the alphabet. You have to cover indentation is your learning of the language in general.

watch this about loops:
https://www.youtube.com/watch?v=6iF8Xb7Z3wQ
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020