What happens when you are updating the value of some variable?

Thank you again,

As I mentioned in my original question. This is a first for me and a VERY dauting undertaking as I am unskilled in every basic facet of what programming is. I may ask many questions, most of them rudimentary and wrong but I appreciate all of the advice in advance. First I need to find the onion, then I will start peeling.

1 Like

Fair enough. With you.

The spelling of lenght should be length, I had the same error lol, happy learning!

This is a good example of mutability where your variable can have a value that changes as needed as opposed to an immutable one where the value is fixed and never changing

I actually created a third variable - quilt_dim = quilt_length * quilt_width
But whenI updated the quilt_length to 8, the value of quilt_dim syill remained 96 (should have been 64). What went wrong?

My code:

quilt_width = 8
quilt_length = 12
quilt_dim = quilt_width * quilt_length
print(quilt_dim)

updating quilt length

quilt_length = 8
print(quilt_dim)

Output:
96
96

Why? The calculation gives a result. This result doesn’t change unless you re-run the calculation

I am not sure why I am getting the same value. I ran the code multiple times but ended with 96 as the uptput after changing quilt_length to 8

And why should changing quilt_length affect quilt_dim?

quilt_dim hasn’t changed, and it won’t change unless you change/re-assign it

Why is the calculation with a * than a + ?

Multiplication is the fast way to add a given number, N times.

6 + 6 + 6 + 6 + 6 + 6  ==  36
                6 * 6  ==  36

It would take 216, 6’s to add up to, 6 ** 4.

Apologies I phrased that incorrectly.

Why is the solution print (quilt_width * quilt_lenght) and not (quilt_width + quilt_lenght)??

For the first question of, " Let’s make this first quilt 8 squares wide and 12 squares long. Print out the number of squares you’ll need to create the quilt!"

The first solution is used to compute the area of the quilt in terms of square units. If it is 8 squares wide (that’s 8 squares set side by side in row) then the area will be 12 rows of 8, or 8 * 12 (a product) giving 96 square units.

quilt_width + quilt_length

is a sum that will give us one-half of the perimeter of the quilt. The perimeter is the length along the outside edges.

2 Likes

Although previous posts have correctly answered the questions raised by ’ tag6763830753’, and ‘cloud4251584065’ (these usernames are not presented in response to ‘@’), I thought it may be helpful to be more explicit in case other new coders also struggle with this ‘concept’.

First off, I suspect some people familiar with spreadsheet formulae are confusing the way a spreadsheet formula works with the way a program formula works, wherein a (cell) formula dynamically updates when the underlying data value changes. (E.g. if cell A1 contains the formula “=A3+A4”, and A3 contains “23” and A4 contains “45”, cell A1 will display a value of “68”, but, the cell still contains the formula (which displays in the formula bar) - which is why the displayed value changes when a value in cell A3 or A4 changes.

When a variable in Python (and most programming languages) is assigned the (value of a) formula, the formula is evaluated immediately and the result is what is stored in the variable, not the formula. Thus:

quilt_width = 8
quilt_length = 12
quilt_dim = quilt_width * quilt_length

which evaluates to:

quilt_dim = 8 * 12

which evaluates to

quilt_dim = 96

i.e. quilt_dim at this point contains the result of the formula, namely 96, and not the formula. In terms of variable behaviour it is no different to quilt_width or quilt_length.

Therefore, the same way as when you want quilt_length to contain a new value, you had to insert code to assign the new value to it, if you want quilt_dim to contain a new value, you have to insert code for that as well, whether it is in the form of a literal (like 64), or a formula.

I hope this is helps someone.

Here is my solution:

quilt_width = 8
quilt_length = 12
print (quilt_width * quilt_length)
quilt_length = 8
print (quilt_width * quilt_length)