[英]Python: How do I add variables with integer values together?
I'm new to Python. 我是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. 我想要平衡和存款一起添加到它1050,但我只是得到1000.我知道我显然没有正确格式化它(余额+存款),但我无法弄清楚正确的格式化方式。
Thanks. 谢谢。
Doing this: 这样做:
balance + deposit
Does the addition and returns the result (1050). 是否添加并返回结果(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
而不是使用新变量,可以使用+=
运算符 :
balance += deposit
This is equivalent to doing: 这相当于:
balance = balance + deposit
You need to assign the sum to a variable before printing. 您需要在打印前将总和分配给变量。
balance = 1000
deposit = 50
total = balance + deposit
print "Balance: " + str(total)
您需要使用赋值运算符:balance = balance + deposit OR balance + = deposit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.