Python Forum
How to return values from For Loop via return in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to return values from For Loop via return in a function
#7
(Jan-05-2019, 02:11 PM)stullis Wrote: You've encountered two problems. First, the error you were receiving was due to assigning multiple variables and having too many variables to unpack. Like this:

def return_3_vars():
    return (1,2,3)

a, b = return_3_vars() # Assigns two variables but has three values to assign; raises error
The new problem you're encountering is that variable c is a list, not a single value. The comparisons are testing for a single string. You need to loop over c or slice it:

def main():
    c = detected_objects_2()

    for value in c:
        if value == "['turnLeft']":
            print("Turn Left is working!!!")
        elif value == "['turnRight']":
            print("Turn Right is working!!!")
        else:
            print("NO DETECTION at all!!!")  
 
main()

I tried what you said but still I cannot see the results in python shell.

no results still

When I add print(b_values) inside of for loop, it generates but still when I go to other images, it makes a long array list inside of the same life. Otherwise, the code you wrote in the main are not working at all by python to show results in python shell.

[Image: jzMrZf1]
Reply


Messages In This Thread
RE: How to return values from For Loop via return in a function - by AykutRobotics - Jan-05-2019, 02:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex find string then return 10 character after it pyStund 6 1,606 Aug-04-2022, 11:26 PM
Last Post: Pedroski55
  Matplotlib scatter plot in loop with None values ivan_sc 1 2,297 Nov-04-2021, 11:25 PM
Last Post: jefsummers
  Sympy nonlinsolve does not return explicit solution axelle 1 2,274 Jan-20-2021, 11:38 AM
Last Post: Serafim
  A function to return only certain columns with certain string illmattic 2 2,231 Jul-24-2020, 12:57 PM
Last Post: illmattic
  Match string return different cell string Kristenl2784 0 1,440 Jul-20-2020, 07:54 PM
Last Post: Kristenl2784
  Basic storage of function values for monte carlo simulation glidecode 1 1,778 Apr-15-2020, 01:41 AM
Last Post: jefsummers
  how to get x values based on y-axis values from curvefit function python_newbie09 1 3,312 Sep-19-2019, 02:09 AM
Last Post: scidam
  Comparing Values Resulting from Function Outputs firebird 0 1,829 Jul-25-2019, 05:16 AM
Last Post: firebird
  Return ERROR when installing pyopencl Helcio_Sarabando 1 4,183 Sep-08-2018, 11:23 PM
Last Post: Larz60+
  Converting days to years in loop while computing values across grid cells Lightning1800 2 2,662 May-15-2018, 08:44 PM
Last Post: Lightning1800

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020