简体   繁体   中英

Storing values from a for loop

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)

I am really new to python. So I am trying to get value from the get_laser_summit and store it on e. is this the right way to do

as @Sujay said you can not iterate over integer like 20,200,400, it is possible they are in list like [20,200,400], second you append function return None so you can not assign to it any value, you have to get value then append it

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 

Your code also right but there can be error if list size change so with you code

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

if you dont wan't to be list so you can use *

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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