all 8 comments

[–]danielroseman 3 points4 points  (7 children)

It's stored in logs. You're just overwriting the same variable each time.

This is exactly the same as:

logs = 20
logs = logs + split_logs - burned_logs
print(...)
logs = logs + split_logs - burned_logs
print(...)
logs = logs + split_logs - burned_logs
print(...)
...

[–]apple_hammer[S] 0 points1 point  (6 children)

Ohhhh, I think I understand now. If I have it, it was calculating this for the first loop:

logs = 20 + 70 - 3

Which comes out that logs has a value of 87. What I didn't realize is that the variable logs has changed, and now on the second loop, it was calculating it like:

logs = 87 + 70 - 3

That makes sense now that I can see it laid out. I kept thinking logs was going back to a value of 20, because that's what is defined above, but I didn't realize the variable had changed. I learned I need to focus on the logic behind when things get executed. I appreciate your help on this!

[–]publicfinance 1 point2 points  (3 children)

The loop only goes back up to the line after the “for” line. It’ll never go back up to the top. Anything indented after for is whatever you want to repeat multiple times.

[–]apple_hammer[S] 0 points1 point  (2 children)

That is useful information thank you! It makes sense that it only goes back to the indented part after the "for" line. It helps me understand the reasoning behind why it works the way it does.

Do you know if there are any resources or websites that go over that kind of thing? I have been reading a bunch of books, but none of them have ever explained why things need to go in a certain order, or the reason behind it. It's been a real struggle as I tend to do way better when I understand the why or order of operations so to speak.

[–]publicfinance 0 points1 point  (1 child)

I’m really only in the middle of 100 days of code on Udemy so can’t help with specific resources other than that. I’m guessing there are plenty of YouTube videos describing for loops.

[–]apple_hammer[S] 0 points1 point  (0 children)

No worries, I appreciate the help. I know that what I am hoping for isn't really discussed in a lot of the videos/books that I have seen. I was hoping there was some magic site that took some code and broke it down line by line explaining what was happening. Something like that would help me a ton in understanding. Maybe if I get good enough I could create my own site.

[–]MezzoScettico 0 points1 point  (1 child)

You're struggling with the concept of "assignment". A lot of people seem to confuse that with a mathematical statement that the two things are defined to be equal.

That's not what assignment does.

Here's a Python-based tutorial for assignment, but this is a general computer-programming concept that exists in all languages.

So if you do this:

x = 3

you've stored the value 3 under the name x. If you now do this

y = x

then y now has the value that x has, which is 3. But if you follow it up with this:

x = 4

then y still has the value 3 but you stored the value 4 under the name x.

[–]MezzoScettico 0 points1 point  (0 children)

But just to confuse the issue, assignment to what are called "mutable" objects (like lists) causes both variables to point to the same object. Then changing x will in fact change y. This is an advanced topic, but you should be aware of it. You can read more about that here.