Why change a variable rather than just create a new one?

Why would we change the value of a variable rather than create a new variable? I can understand if the variable is defined by something happening in real-time. Like a temperature reading. In the course it is not clear why and what the purpose would be to simply change “lunch” to something else.

10 Likes

Sorry this is a late answer.

The reason behind this isn’t really evident yet in the course but becomes valuable when you begin learning functions and loops. This gives the opportunity for a function to always print the variable even if it gets changed throughout your code, or for a function to regularly change a variable then print it.
It can also be valuable to keep the number of variables lower so your program can run more efficiently. Something more relevant when your code becomes long and complex. Both of these should make more sense later on.

16 Likes

A previously defined variable can’t seem to pick up the new variable I assigned:
please refer to the screenshots below


The only difference between the two outcomes is whether the line

my_team = my_number%4

exist under the newly assigned variable.

How should I interpret this outcome?
Thank you

2 Likes

I think that your 2nd example is working the way it is because you essentially set my_team equal to the integer 3 on line 15 then you simply print that variable on line 22.

I believe you’re thinking that my_team variable is set to be dependent upon the value of the my_number variable, but it doesn’t quite work that way.

Thank you so much~
the solution I come up with is to just pack the whole thing into a function and set

my_number

as an argument.
Would there be other solution to this problem?

I think your program has worked fine. You have not assigned an updated value to my_team, as such, the earlier assigned value printed in line 17 is still in the memory.