我如何将范围放入for循环中?

问题描述 投票:-2回答:1

我正在尝试对函数中值范围内的每个数字求平方。

def RetSquared(Number):
# Returns the square
    range(Number(1,10))
    for item in Number:
        Squared =item ** 2
        return(Squared)

无输出

python loops range
1个回答
0
投票

这应该可以解决问题

def returnSquare(num):
    for i in range(1,num+1):
        val = i**2
        print(val)

例如

returnSquare(12) 

你得到

1
4
9
16
25
36
49
64
81
100
121
144
© www.soinside.com 2019 - 2024. All rights reserved.