【发布时间】:2015-09-08 16:22:31
【问题描述】:
如果一个浮点变量有 5 个数字,其他有 4 个数字,并且对它们执行算术运算,我的代码不会像我预期的那样运行。例如:
def capacity(self):
mmax = self.mmax.get()
current = self.current.get()
mmin = self.mmin.get()
flag = False
if flag is False:
if mmax.isdigit() and mmin.isdigit() and current.isdigit():
capacity = float(100 * ((float(current) - float(mmin)) / (float(mmax) - float(mmin))))
if mmin <= current <= mmax and 0 <= capacity <= 100:
flag = True
else:
self.result = str("Please ensure the values entered correspond to the correct value.")
else:
self.result = str("Please enter only positive integers.")
if flag is True:
self.result = "Capacity: %2.2f" % capacity + '%'
如果mmax = 10000, current = 5000, and mmin = 1000,self.result = str("Please ensure...")。
如果mmax = 9999, current = 5000, and mmin = 1000,self.result = str("Capacity: 44.45%)。
这是怎么回事/我该如何克服这个问题?
【问题讨论】:
-
floats 很多...你能澄清一下你的期望吗?另请注意,例如if not flag:优于if flag is False:,因为它更好地处理 不是False的 false-y 值。 -
您应该检查
mmax、current和mmin的值是否确实符合您的预期。如果我只是强制它们为 10000、5000 和 1000,它会给出正确的结果。 -
@spectras 这是我从一个返回字符串的 tkinter 条目中获得
mmax、current和mmin的问题,我在转换为浮点数之前直接对它们进行算术运算/整数。 @jonrsharpe 注意到了。
标签: python tkinter floating-point size tkinter-entry