存储for循环中的值


e=[]
rc = RobotControl(robot_name="summit")
def fun (a,b,c):
for d in a,b,c:
e.append(d)= rc.get_laser_summit(d)
print(e)`enter code here`


fun(20,200,400)

我对蟒蛇真的很陌生。所以我试图从get_laser_summit中获取值并将其存储在e上。这是进行的正确方法吗

正如@Sujay所说,你不能像20200400那样迭代integer,它们可能像[202000400]一样在列表中,其次你的append函数返回None,所以你不能给它分配任何值,你必须获得值,然后将其附加到

e=[]
rc = RobotControl(robot_name="summit")
def fun (lst):
for d in lst:
value_from_func = rc.get_laser_summit(d)
e.append(value_from_func)    
print(e)

fun([20,200,400]) #[] list can take more a,b,c,d 

你的代码也是正确的,但如果列表大小发生变化,可能会出现错误,所以用你的代码

e=[]
rc = RobotControl(robot_name="summit")
def fun (a,b,c):
for d in a,b,c:
value_from_fun = rc.get_laser_summit(d)
e.append(value_from_fun)
print(e)

fun(20,200,400) 
fun(20,200,400,500) # now this will give error

如果你不想被列入名单,那么你可以使用*

e=[]
rc = RobotControl(robot_name="summit")
def fun (*lst):
for d in lst:
value_from_fun = rc.get_laser_summit(d)
e.append(value_from_fun)
print(e)

fun(20,200,400)
fun(20,200,400,500) # now this will also work 

最新更新