Amanda
Amanda

Reputation: 51

Python: How do I add variables with integer values together?

I'm new to Python. How do I add variables with integer values together?

balance = 1000
deposit = 50
balance + deposit
print "Balance: " + str(balance)

I want balance and deposit to add together to it 1050, but I'm just getting 1000. I know I'm clearly not formatting it (balance + deposit) correctly, but I can't figure out the right way to format it.

Thanks.

Upvotes: 4

Views: 71892

Answers (3)

steliosbl
steliosbl

Reputation: 8921

Doing this:

balance + deposit

Does the addition and returns the result (1050). However, that result isn't stored anywhere. You need to assign it to a variable:

total = balance + deposit

Or, if you want to increment balance instead of using a new variable, you can use the += operator:

balance += deposit

This is equivalent to doing:

balance = balance + deposit

Upvotes: 11

The_Outsider
The_Outsider

Reputation: 1925

You need to assign the sum to a variable before printing.

balance = 1000
deposit = 50
total = balance + deposit
print "Balance: " + str(total)

Upvotes: 2

Marco Simone
Marco Simone

Reputation: 41

you need to use the assignment operator: balance = balance + deposit OR balance += deposit

Upvotes: 0

Related Questions