如何在Python中检查某物是否是x折叠的?
所以,我正在努力制作这个节目,我现在已经非常接近了,但是我不能完成最后的润色。我追踪了我的问题,因为问题在于模的使用。我试图得到e的五倍,但当我这样做时,我的列表中的第三个元素得到-2,这不是我所期望的 这可能是因为我试图用模除负数,但我无法解决这个问题,因为我不知道如何解决。有人能帮我解决这个问题吗如何在Python中检查某物是否是x折叠的?,python,Python,所以,我正在努力制作这个节目,我现在已经非常接近了,但是我不能完成最后的润色。我追踪了我的问题,因为问题在于模的使用。我试图得到e的五倍,但当我这样做时,我的列表中的第三个元素得到-2,这不是我所期望的 这可能是因为我试图用模除负数,但我无法解决这个问题,因为我不知道如何解决。有人能帮我解决这个问题吗 def f10(start, n): """ The parameters start and n are both int's. Furthermore n > 0.
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]
非常感谢您的帮助。这里有一些问题。最重要的是,您正在尝试使用变量e来迭代循环和存储计算结果 试试这个:
def f10(start, n):
x = start
new_list = []
for i in range(n):
new_list.append(x)
x = x-4 if x%5 else x//5
return new_list
这里有一些问题。最重要的是,您正在尝试使用变量e来迭代循环和存储计算结果 试试这个:
def f10(start, n):
x = start
new_list = []
for i in range(n):
new_list.append(x)
x = x-4 if x%5 else x//5
return new_list
您必须使用以前的值来计算新值
def f10(start, n):
result = [start] # I put first element
for _ in range(n-1): # I have to put other n-1 elements yet
if start % 5 == 0:
start //= 5
else:
start -= 4
result.append(start)
return result
# --- compare result with expected list ---
print(f10(1, 10) == [1, -3, -7, -11, -15, -3, -7, -11, -15, -3])
# True
print(f10(9, 12) == [9, 5, 1, -3, -7, -11, -15, -3, -7, -11, -15, -3])
# True
编辑:如果不需要
range()
则可以使用而len(result)
def f10(start, n):
result = [start]
while len(result) < n:
if start % 5 == 0:
start //= 5
else:
start -= 4
result.append(start)
return result
def f10(启动,n):
结果=[开始]
而len(结果)def f10(start, n):
result = [start] # I put first element
for _ in range(n-1): # I have to put other n-1 elements yet
if start % 5 == 0:
start //= 5
else:
start -= 4
result.append(start)
return result
# --- compare result with expected list ---
print(f10(1, 10) == [1, -3, -7, -11, -15, -3, -7, -11, -15, -3])
# True
print(f10(9, 12) == [9, 5, 1, -3, -7, -11, -15, -3, -7, -11, -15, -3])
# True
编辑:如果不需要
range()
则可以使用而len(result)
def f10(start, n):
result = [start]
while len(result) < n:
if start % 5 == 0:
start //= 5
else:
start -= 4
result.append(start)
return result
def f10(启动,n):
结果=[开始]
而len(结果)e=float(e)/5
我不会那样做。为什么不e/=5
?您不希望k中的e正确浮动(注意:它不会更改结果,但仍然)?这不是您应该如何设置e
。在循环中,您将k
中的值分配给e
,但在计算中必须使用前面的e
。您的意思是什么?为什么我不能这样设置呢?e=float(e)/5
我不会那样做。为什么不e/=5
?您不希望k中的e正确浮动(注意:它不会更改结果,但仍然)?这不是您应该如何设置e
。在循环中,您将k
中的值分配给e
,但在计算中必须使用前面的e
。您的意思是什么?为什么我不能这样设置呢?没关系。知道了!谢谢,我喜欢简单的样式。因为result需要n
元素,并且我在result=[start]
中的for
循环中,在result=[start]
前面放了第一个元素?我的意思是,你能在for中提供一个不使用range的方法吗?range是最自然的方法-但是你可以使用while
和一些变量来计算元素。或者使用while len(result)
没关系。知道了!谢谢,我喜欢简单的样式。因为result需要n
元素,并且我在result=[start]
中的for
循环中,在result=[start]
前面放了第一个元素?我的意思是,你能在for中不使用range提供一个吗?range是最自然的方法-但是你可以使用while
和一些变量来计算元素。或者使用while len(result)