FAQ: Learn Python - Python Syntax - Updating Variables

2 posts were split to a new topic: Is += a Shortcut for x = x + y?

2 posts were split to a new topic: Can I Use One Sentence to Add All Remaining Months?

2 posts were split to a new topic: It Won’t Let Me Pass, What Am I Supposed to Do?

2 posts were split to a new topic: Is this a Good Way to Solve It?

2 posts were split to a new topic: Why Are += And =+ Different?

2 posts were split to a new topic: What Does -= Mean?

I think there is supposed to be some code that initially shows up that I’m meant to edit, but I only get the code carrying over from previous exercises. When I click the link to the exercise at the top of the forum page it takes me to a version of the exercise with a bunch of predefined variables. Help?

1 Like

It sounds like you were able to troubleshoot an issue, I don’t run into the problem myself when i transition between the exercises

i can not figure out what is wrong with my code on exercise 7 updating variables

here is my code

january_to_june_rainfall = 1.93 + 0.71 + 3.53 + 3.41 + 3.69 + 4.50
annual_rainfall = january_to_june_rainfall

july_rainfall = 1.05
annual_rainfall += july_rainfall

august_rainfall = 4.91
annual_rainfall += august_rainfall

september_rainfall = 5.16
annual_rainfall += september_rainfall
october_rainfall = 7.20
annual_rainfall += october_rainfall
november_rainfall = 5.06
annual_rainfall += november_rainfall
december_rainfall = 4.06
annual_rainfall += december_rainfall

CAN SOMEONE PLEASE TELL ME WHAT I AM DOING WRONG?..WONT LET ME GO ON.

@net3229678455, I reset the workspace here, and pasted your code in. It worked fine, producing the magic yellow “Next” button. So I don’t know what the problem is.

Have you tried the reset?

why does this work
annual_rainfall += november_rainfall + december_rainfall + october_rainfall + september_rainfall

and this doesn’t work
annual_rainfall = november_rainfall + december_rainfall + october_rainfall + september_rainfall
wouldn’t that be fine too?

i was learning JavaScript not too long ago and i was sticking to that for a little while and for variables this seems a little similar to JavaScript as far as variable go just you don’t need to define what type of variable like var or let.

usually if i wanted to add variables (num) together it would be this
let i = 1
let t = 2
let s = 3
let it = i + t + s

but for python it would be this

i = 1
t = 2
s = 3
it += i + t + s right?

:confused:

i might be making this a little complicated for no reason

those two ways are very different, here:

annual_rainfall += november_rainfall + december_rainfall + october_rainfall + september_rainfall

you add the rainfall of 4 months to annual_rainfall. Don’t forget that += is a shorthand for:

annual_rainfall = annual_rainfall + november_rainfall + december_rainfall + october_rainfall + september_rainfall

in the second code example, you overwrite the value already store in annual_rainfall and set it to the result of the sum.

ohhhhhhhhhh i get it now thank you

i forgot that += multiplied itself with the value you put in it

its not really multiplying, its adding. But point came across :slight_smile:

how to add them all at once?

Please let us be very specific and simple here in explanation so that we can grab the concept. I am still struggling here. What is the difference between += and + ?
Also, what are we expected to do with the annual rainfall exercise? Am still confuced

Can someone please help out? Why are we not including July and August? Is it because of the instruction’s requirement?

+= is a short-hand, lets say we have:

let x = 3;

then:

x = x + 3

and:

x += 3

are the same thing. so + does sum, = is assignment.