Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Python Data Types Del

Alistair Murphy
Alistair Murphy
743 Points

can you add a variable somewhat like this total = new releases + planned (for example)

This would be useful if i know how as it didn't work, so what ever the "new releases" & "planned" were the total variable would adapt the maths to give you the ...total ?

3 Answers

andren
andren
28,558 Points

I'm not quite sure I'm understanding the question, but are you asking if you can use variable names when using math operations, something like this:

a = 5
b = 10
total = a + b

If so the answer is yes, that is completely valid and common, with the code above total would end up containing 15.

The reason

total = new releases + planned

Would not work is that new releases is not a valid name as spaces are not allowed in variable names. In python it's common to use underscores in place of spaces when creating variable names that consist of multiple words. Like this:

new_releases = 50
planned = 12
total = new_releases + planned

The above code would work correctly.

If you have further questions, or if I misunderstood your post, then feel free to reply with further questions. I'll do my best to answer whatever might be confusing you.

Alistair Murphy
Alistair Murphy
743 Points

And if the value changed for new_releases to say 40 then the total would take that into account ? (total = 52) instead. thanks btw that is what i meant. :)

andren
andren
28,558 Points

It does seem like I misunderstood slightly, when you assign a variable to another variable you (generally) just assign it whatever value was in the variable, not the variable itself. Which means they do not update themselves dynamically. In the code examples above the total variable would not change automatically if new_releases or planned was changed later in the program unless you manually reassigned it at that point.

For dynamically calculating values you generally use functions, not variables. Which is a concept I'm guessing you have not been introduced to yet.

Just as an example here is something that works somewhat like you seem to want:

def total():
    return new_releases + planned

new_releases = 10
new_releases = 5
total() # This would return 15

new_releases = 45
new_releases = 10
total() # This would return  55

The above is a simple example, and doesn't really follow best-practices, in reality you would likely create a class in a situation like this where the function and variables are linked like that, but that would make the example a good bit harder to understand since you don't know anything about classes.

Once you have completed a couple of courses here on Treehouse you should easily be able to setup something that works in the way you describe on your own, so I'd recommend just sticking with the courses for now and you'll learn lots of useful cool stuff.

Alistair Murphy
Alistair Murphy
743 Points

Thank you for your time, i hear you, and will stick to the course, thanks for the answers they were brilliant.