5 回答

TA贡献1780条经验 获得超5个赞
要生成系列,您需要做两件事:
将打印放在循环中以在每次迭代时输出累加器变量的值
加 3 来总结每次迭代而不是 1,因为它是系列成员之间的差异
n = int(input("Enter n: ")) # n=8 should work
sum = 1
i = 1
while i <= n:
print(str(sum)+",")
sum = sum +3
i = i+1

TA贡献2012条经验 获得超12个赞
我想你想要这样的东西:
n = int(input("Enter n: "))
series_sum = 0
i = 1
series = []
add = 3
while i <= n:
series.append(i)
series_sum = series_sum + i
i = i + add
print("series: ", series)
print("The sum is" , series_sum)
这将为您提供一个系列(和元素总和),最后一个元素小于n,从开始i = 1并递增add = 3

TA贡献1784条经验 获得超9个赞
每次使用 时,您都希望将计数增加 3 i += 3。
def createList():
user_input = input()
i = 1
list_of_vals = []
while i < int(user_input): # The max value:
list_of_vals.append(i)
i += 3
return list_of_vals
print (createList())

TA贡献1803条经验 获得超6个赞
n = int(input("Enter n: "))
count = 0
i = 1
sum = 0
while count <= n-1:
print(i)
sum += i
i += 3
count += 1
print("Sum is", sum)
添加回答
举报