繁体   English   中英

Python,闲置-我该如何修正编码?

[英]Python, idle - How should I fix my coding?

算术级数是一个数字序列,其中任何两个连续数字之间的距离(或差)相同。 在顺序1、3、5、7,...中,距离为2,而在顺序6、12、18、24,...中,距离为6。

在给定正整数距离和非负整数n的情况下,创建一个列表,该列表由1和n之间(并包括)之间的算术级数组成,并具有一定距离。 例如,如果distance为2并且n为8,则列表将为[1、3、5、7]。

将列表与变量arith_prog关联。

我应该在哪里修复代码?

arith_prog = []  
n = int()
for i in range(1, n+distance, distance):
    arith_prog = arith_prog + i
arith_prog = list(range(1, n+1, distance))

假设可以包含n

似乎有些奇怪,但通常我认为问题出在您的范围内,只需将其更改为n ,而不是n + distance

arith_prog = []
n = 8
distance = 2
for i in range(1, n, distance):
    arith_prog.append(i)
print(arith_prog)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM