0
Confused about this variable question
It reads: spam=2 eggs=3 del spam eggs=4 spam=5 print (spam*eggs) The answer is 20 but I'm having trouble understanding how the final result is 20 when the only variable that was deleted and replaced was spam. Does this automatically cancel out the first variable for eggs? Thanks
5 Answers
+ 1
so in the lines of code, redefining a variable replaces the value with the most recent definition.
the previous lines dont compute when replaced later in the code. so 5*4 is all you have for the output function. which is to multiply. :)
+ 1
Got it, thanks!
+ 1
You dont need to use del, just asign new value. Thats why they called variables because they value can change.
0
you asigned new value for eggs with the = operator
0
Thanks! Okay, I guess in this case the del wasn't really necessary? I tried it without del and got the same answer.