所以,我正在努力做这个节目,我现在非常接近,但我不能做最后的润色。我追踪了我的问题,因为问题在于模的用法。我试图得到e的五倍,但是当我这样做的时候,我的列表中的第三个元素得到-2,这不是我所期望的。你知道吗
这可能是因为我试图用模除一个负数,但我不能解决这个问题,因为我不知道怎么做。有人能帮我解决这个问题吗?你知道吗
def f10(start, n):
"""
The parameters start and n are both int's. Furthermore n > 0.
The function f10() should return a list of int's that contains n
elements.
The first element (e) in the resulting list has to be start.
The successor of an element e is calculated as follows:
- if e is a fivefold (e.g. n is divisible by 5),
then the next value of e is e / 5
- if e is not a fivefold, then the next value of e is e - 4
Example:
f10(1, 10) should return
[1, -3, -7, -11, -15, -3, -7, -11, -15, -3]
f10(9, 12) should return
[9, 5, 1, -3, -7, -11, -15, -3, -7, -11, -15, -3]
"""
pass
new_list = []
k = range(start, n+1)
e = k[0]
new_list.append(e)
for e in k:
if e % 5 == 0:
e = float(e) / 5
else:
e -= 4
new_list.append(e)
return new_list
print f10(1, 10)
print f10(9, 12)
所以,我应该得到:
[1, -3, -7, -11, -15, -3, -7, -11, -15, -3]
但我明白了
[1, -3, -2, -1, 0, 1.0, 2, 3, 4, 5, 2.0]
我们将不胜感激。你知道吗
必须使用以前的值来计算新值。你知道吗
编辑:如果您不想要
range()
,那么您可以使用while len(result) < n:
这里有一些问题。最重要的是,您正在尝试使用变量e来迭代循环和存储计算结果。你知道吗
试试这个:
相关问题 更多 >
编程相关推荐