Python中的奇怪行为?

时间:2017-07-20 10:29:13

标签: python python-3.x loops

我有一个非常简单的程序。

count = 0
total = 0

def iseven(number):
    if number % 2 == 0:
        True
    else:
        False

while count < 10 :
    if iseven(count):
        total = total * 2
    else:
        total = total * 4

    print total
    count = count + 1

print "final total is ", total

但是这只会在每次迭代时打印零,最终总数则为零。 所以看起来总价值没有更新。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

Traceback (most recent call last):
  File "C:\temp.py", line 3, in <module>
    test_dll = windll("C:\\MathLibrary.dll")
TypeError: 'LibraryLoader' object is not callable

你乘以0,总是得到0

输出:

    add = test_dll.add
  File "C:\Python27\lib\ctypes\__init__.py", line 375, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python27\lib\ctypes\__init__.py", line 380, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'add' not found

答案 1 :(得分:0)

您的is_even函数缺少返回语句。

此外,total应初始化为1,而不是0(1是乘法的幂等值,而不是0)。