FAQ: Learn Python - Python Syntax - Updating Variables

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.

Hi!I am a beginner.I wanted to know why do we need to leave a blank line after each string.Thanks.

I just started this week and I am someone who is very much a beginner. Was 3, 6, and 9 strings in exercise 7 missing in anyone elses? Also I need help creating a new string also. Can anyone help?

a = b + c + d + e + ... # a, b, c, d, e each represents a variable

to create a new string:

strng = ""

As for other exercises, please add specific questions in the corresponding thread or post a specific question regarding a link or code to the exercise with proper formatting!