Changing Numbers: Variables Not Updating

This HAS to be something on my end because I’m running into the same issue in PyCharm. I’m currently going through the Python 3 lessons, and I’m on Changing Numbers, where you change the value of a variable halfway through.

The 1st objective is to figure out how many squares you need to complete a quilt, by multiplying the width (8) by the height (12) of the quilt.

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

Done! It prints 96!

The second objective says, “Oops! This project is bigger than we expected, so let’s change the length from 12 to 8.” Ok!

quilt_length = 8
print(total_squares)

Answer: 96.

It still says it’s correct enough to let me move on to the next lesson, but I don’t want to move on when I know it’s not the right answer, because I’m not sure what’s causing it.

Here’s the full:

#Prints 96
quilt_width = 8
quilt_length = 12
total_squares = quilt_width * quilt_length
print(total_squares)

#Still prints 96, even though we know 8*8 is 64
quilt_length = 8
print(total_squares)

Thanks for your help! This is a weird one for me because there’s no official errors, but I know it’s an error.

2 Likes

total_squares is assigned the value of quilt_width * quilt_length prior to your first print(total_squares). Then you reassign quilt_length a value of 8, but total_squares has not been reassigned. That line of code has already been executed, so total_squares still points to the value 96. The exercise actually only asks you to create 2 variables, and print the number of squares needed:

You’ve decided to get into quilting! To calculate the number of squares you’ll need for your first quilt let’s create two variables: quilt_width and quilt_length . 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!

If you do it like this, you’ll print the results using the updated quilt_length:

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

quilt_length = 8
print(quilt_width * quilt_length)

Or you could stick to your original plan, but add a line of code:

#Prints 96
quilt_width = 8
quilt_length = 12
total_squares = quilt_width * quilt_length
print(total_squares)

#Now prints 64
quilt_length = 8
total_squares = quilt_width * quilt_length #add this line
print(total_squares)

Later, you’ll learn to use functions which will streamline this process, and eliminate redundant code. Hope this helps! Happy coding!

2 Likes

Wow! I see it now. Thanks a lot! That was really helpful!

1 Like