【发布时间】:2013-03-25 20:59:57
【问题描述】:
#iterative program to find the highest odd number
m = sorted([a, b, c, d, e, f, g, h, j, k])
n = max(m)
n = int(n)
count = 10
while n%2==0:
m=[m[:-1]]
n = max(m)
n = int(n)
count =-1
if count==0:
print 'There are no odd numbers'
else:
print str(n), 'is the largest odd number'
我输入了包含奇数的变量,它给了我正确的答案,但是当我输入所有偶数以满足'count==0'条件时,出现以下错误:
TypeError: int() 参数必须是字符串或数字,而不是“列表”
我不明白为什么输入奇数时不会出现此错误。
【问题讨论】:
-
a、b、c、d、e、f、g、h、j的值是多少k?此外,考虑到您正在对列表进行排序,这个算法似乎不必要地复杂。 -
尝试每次循环打印
m和n的值;它应该可以帮助您了解错误所在。也就是说,这是一种非常非正统的迭代值列表的方式。
标签: python while-loop iteration typeerror