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

前端 未结 3 980
无人共我
无人共我 2021-01-14 13:15

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

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


        
相关标签:
3条回答
  • 2021-01-14 13:28

    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
    
    0 讨论(0)
  • 2021-01-14 13:37

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

    0 讨论(0)
  • 2021-01-14 13:41

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

    balance = 1000
    deposit = 50
    total = balance + deposit
    print "Balance: " + str(total)
    
    0 讨论(0)
提交回复
热议问题