Can we add string with numeric values we updated using the “+=”, if yes how ?
for eg
if abc =10
abc +=5
abc += 10
abc += “string” ?? (here can we concatenate the value with the number added.)
print(abc)
Instead of writing number_of_miles_hiked = 12
number_of_miles_hiked += 2
print(number_of_miles_hiked) , can’t we instead just add 2 to the first variable, i.e.,
number_of_miles_hiked = 12 + 2
print(number_of_miles_hiked)
They mention that “we keep a running count of the number of miles a person has gone hiking over time.” If we were to create a lap time for each extra mile, then =+ makes sense. Is that what they’re trying to say? If so, in what other situations will this be used?
Also, can anyone explain with code about this statement: “Instead of recalculating from the start, we keep a grand total and update it when we’ve gone hiking further.”? Thank you.
These concepts are fundamental, you will see that you need them later once you start doing porjects.
Sure, this example is a bit lame, but we can’t make you do a complicated project even though this would show the more practical side of updating existing variables.
Hi, the += can be used to add more variables at a time as written in the FAQ. Within the python exercise with the following alternative answer I always get a bug… Is someone able to explain the difference? Thanks!!
total_price += nice_sweater += fun_books (=> this leads to an error). INSTEAD of the suggested code:
The print statement reads “print(“The total price is”, total_price)” where there is no space after the word “is” in the string, but when you run the code the sentence it outputs contains the proper space. Just a miniscule error I noticed today!