在python中用正方形列出理解

测验:使用列表推导创建一个平方数列表(n * n)。该函数接收变量start和end,并返回在start和end(包括两端)之间的连续数字的平方的列表。例如,squares(2,3)应该返回[4,9]。 我的代码:

使用列表推导创建一个平方数列表(n * n)。

该函数接收变量start和end,并返回连续数字的平方的列表

在开始和结束之间。例如,squares(2,3)应该返回[4,9]。

enter code here
def squares(start, end):
    squares = [value ** 2 for value in range(0, 11)]
    return [squares]
print(squares(2, 3)) # Should be [4, 9]
print(squares(1, 5)) # Should be [1, 4, 9, 16, 25]
print(squares(0, 10)) # Should be [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
评论
大宝剑
大宝剑
def squares(start, end):
    squares = [value ** 2 for value in range(start,end+1)]
    return [squares]
print(squares(2, 3)) # Should be [4, 9]
print(squares(1, 5)) # Should be [1, 4, 9, 16, 25]
print(squares(0, 10)) # Should be [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

[[4, 9]]
[[1, 4, 9, 16, 25]]
[[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]]
点赞
评论
April
April

尝试这个:

def squares(start, end):
    squares = [value ** 2 for value in range(start, end+1)]
    return squares
print(squares(2, 3)) # Should be [4, 9]
print(squares(1, 5)) # Should be [1, 4, 9, 16, 25]
print(squares(0, 10)) # Should be [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

输出:

[4, 9]
[1, 4, 9, 16, 25]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
点赞
评论