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.
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.
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?
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
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.
hey, do you mind sharing the code of your solution to this problem? thanks!
Forum policy is to not post full solutions to encourage people to figure out the exercises themselves rather than copying off of someone else’s answer. However, if you have any questions, you can ask them here and we would be more than happy to help out.
Welcome to the forums!
We always try to reduce the use of unnecessary variables as larger the number of variables, more the code will become messy and it will also use more memory making overall code takes more to run.