Python:为什么变量不更新?
我正在调试的 python 程序具有以下代码(包括用于调试的 print
语句):
print "BEFORE..."
print "oup[\"0\"] = " + str(oup["0"])
print "oup[\"2008\"] = " + str(oup["2008"])
print "oup[\"2009\"] = " + str(oup["2009"])
oup0 = oup["0"]
oup2008 = oup["2008"]
oup2009 = oup["2009"]
ouptotal = oup2008 + oup2009
print "ouptotal = " + str(ouptotal)
if ouptotal > 0:
oup["2008"] = oup2008 + oup0 * (oup2008 / ouptotal)
oup["2009"] = oup2009 + oup0 * (oup2009 / ouptotal)
print "AFTER..."
print "oup[\"0\"] = " + str(oup["0"])
print "oup[\"2008\"] = " + str(oup["2008"])
print "oup[\"2009\"] = " + str(oup["2009"])
直到此时,变量都会正确更新。当我运行该代码时,屏幕上显示以下内容:
BEFORE...
oup["0"] = 22032
oup["2008"] = 541
oup["2009"] = 15223
ouptotal = 15764
AFTER...
oup["0"] = 22032
oup["2008"] = 541
oup["2009"] = 15223
为什么 oup["2008"] 和 oup["2009"] 不更新?
(“Jaunty”Ubuntu 机器上的 Python 版本为 2.6.2。)
A python program that I'm debugging has the following code (including print
statements for debugging):
print "BEFORE..."
print "oup[\"0\"] = " + str(oup["0"])
print "oup[\"2008\"] = " + str(oup["2008"])
print "oup[\"2009\"] = " + str(oup["2009"])
oup0 = oup["0"]
oup2008 = oup["2008"]
oup2009 = oup["2009"]
ouptotal = oup2008 + oup2009
print "ouptotal = " + str(ouptotal)
if ouptotal > 0:
oup["2008"] = oup2008 + oup0 * (oup2008 / ouptotal)
oup["2009"] = oup2009 + oup0 * (oup2009 / ouptotal)
print "AFTER..."
print "oup[\"0\"] = " + str(oup["0"])
print "oup[\"2008\"] = " + str(oup["2008"])
print "oup[\"2009\"] = " + str(oup["2009"])
Up until that point, the variables update correctly. When I run that code, I get the following on the screen:
BEFORE...
oup["0"] = 22032
oup["2008"] = 541
oup["2009"] = 15223
ouptotal = 15764
AFTER...
oup["0"] = 22032
oup["2008"] = 541
oup["2009"] = 15223
Why aren't oup["2008"] and oup["2009"] updating?
(Python version is 2.6.2 on a "Jaunty" Ubuntu machine.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果值是整数,则
(oup2008 / ouptotal)
将为零,因此它们将更新为自己的值 + 0,因此没有变化。将它们转换为浮点数进行计算,然后根据需要返回浮点数,并且它应该按预期工作。
例子:
If the values are integers, then
(oup2008 / ouptotal)
will be zero, so they will be updated to their own value + 0, hence no change.Convert them to floats for the calculation, then back if required, and it should work as expected.
Example:
整数除法产生整数结果。可以在除法项之一上使用
float()
或在顶部添加from __future__ import div
。Integer division results in integer results. either use
float()
on one of the division terms or addfrom __future__ import division
at the top.它们是,您只需用相同的值更新它们即可。在Python 2.6中,
/
仍然是整数除法运算符(参考),因此除(oup2008 / ouptotal)
就是用一个数字除以一个更大的数字,结果总是为零。如果您想要更高版本的行为,如果/
是浮点运算符,您可以通过 future 语句导入division
来获得它。They are, you're just updating them with the same value. In Python 2.6,
/
is still an integer division operator(reference), so dividing(oup2008 / ouptotal)
is dividing a number by a bigger number, which always results in zero. If you want the behavior from later versions, were/
is a floating-point operator, you can get it with a future statement importingdivision
.