How to return values from a "for" loop?

how to get return value from method in java
function with return value in c
python function return variable
how to use return value in another function python
return value javascript
return value python
javascript function return value to variable
python function return multiple values

I am trying to make a loop go through a provided string and return False if a character that is not recognized is found, and True if all the characters are recognized. I do realize that for loops don't allow for returning values within them, but considering this is part of a bigger code I need to return a bool value as opposed to printed.

Is there a way of figuring this issue out without the for loop? My code is below.

(I am also in an intro class, where many of the built in functions and such we have not learned yet and so I should probably not yet use...any simple ways of getting around this?)

    chars = ["o", ".", "\"", "n"]


    def find_characters(s):
        for c in s:
            if c not in chars:
                break

    # IDEALLY WANT
    def find_characters(s):
        for c in s:
            if c not in chars:
                return False
                break
            else:
                return True
def find_characters(s):
    for c in s:
        if c not in chars:
            return False
     return True

Based on the comments, I think this is what you want.

Returning a Value from a Method (The Java™ Tutorials > Learning , Returning a Value from a Method. A method returns to the code that invoked it when it. completes all the statements in the method,; reaches a return  To return a value from a custom function, you need to use the return keyword. We saw this in action recently in our random-canvas-circles.html example. Our draw() function draws 100 random circles somewhere on an HTML <canvas> :

If your provided string is s, then you can check your condition and return with:

def find_characters(s):
    return all(c in chars for c in s)

6.2. Functions that Return Values, Functions that Return Values¶. Most functions require arguments, values that control how the function does its job. For example, if you want to find the absolute​  The function’s result is sent back in Line 21 by using the return keyword. Exercise 1: Type the source code from A Function That Returns a Value into your editor. Build and run. Functions that return values can have that value stored in a variable, as shown in Line 11 of A Function That Returns a Value, or you can also use the value immediately.

you can use yield to return the multiple values from a loop:

def createGenerator():
    mylist = range(3)
    for i in mylist:
        yield i*i

mygenerator = createGenerator() # create a generator
for i in mygenerator:
     print(i)


#output:
0
1
4

7.4a, Because the variables are evaluated before the function returns, and a copy of the value is returned to the caller, there are no problems when the function's  In this video we are discuss about return value from a function | how to returning value from a function? To watching upcoming video subscribe the channel.

Python Tutorial: Functions, A return statement ends the execution of the function call and "returns" the result, i.e. the value of the expression following the return keyword, to the caller. If the  Passing arguments By Reference is probably the most common way to return multiple values from a function. It uses the ByRef keyword to tell the compiler that the variable passed to the function is only a pointer to a memory location where the actual value of the variable is stored.

12.4 Functions that Return Values, A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle,  To return a value we MUST use the return keyword. The string 'x + y + z' x+y+z calculates a number (assuming x+y+z are numbers) which represents the sum of the values x, y and z.

Activity: Return Values, This ends the call to the function, continuing the code from where the function was called. Concept: Returning a Value. Functions can be given return value by  Console.ReadKey() End Sub Sub ReturnAValue() ' Return a value type with a lambda expression Dim task1 = Task(Of Integer).Factory.StartNew(Function() 1) Dim i As Integer = task1.Result ' Return a named reference type with a multi-line statement lambda.

Comments
  • You want that if it finds at least one character that isn't recognized return True from the for loop?
  • you don't need break if you use return - because break will be never executed. And user return True after for-loop, not inside.
  • Basically. I want that if it finds at least one character that is not recognized that it should return False and stop looking. If everything is recognized I'd like it to return True.
  • you can use return in for-loop - so what is the problem ?
  • I've realized that return would need to be outside of the for loop... so below. the issue that I don't know how to fix in that scenario is that no matter what it finds it will always return False. is there someway to get around that? its because if it finds an incorrect character and so i break it out of the loop it return false, yet if it runs through all the characters and they are recognized, it will still reach the return false statement and also return false.
  • Added question: i realize to check for "\" i had to add an extra quotation so that it would be judged as a string, but this also means that the code only recognizes \" as a valid character. any way of avoiding this?
  • change "\"" to '"'(single-quote, double-quote, single-quote). That should fix your issue.
  • this also works. thank you :). Added question: i realize to check for "\" i had to add an extra quotation so that it would be judged as a string, but this also means that the code only recognizes \" as a valid character. any way of avoiding this?
  • @sarahsss: Just use "\\" which is a standard way to represent a single backslash character in a string literal.