6

So as the title suggests, I made a simple function like this:

import sys

string = sys.argv[1]
def position():
        for index in range(len(string)):
                if string[index] == 'A':
                        print(int(index+1))

position()

Which, when used with a test string like "AABABC", will return the position of each "A" as a number.

Here I increment the index with 1 because I noticed the range is starting at 0. While I can pass 1 or any number I want to it to make it start to that, it will remove/not print everything I want. So I found that this worked best in this case.

Everything works here. What doesn't is when I replace the print in the function, with a return statement:

import sys

string = sys.argv[1]
def position():
        for index in range(len(string)):
                if string[index] == 'A':
                        return int(index+1)

print(position())

Here I only show this edit to the code, but I did try a couple of different things, all with a similar results (in that it doesn't work):

  • not using int() and instead incrementing the index with += -> doesn't work in this specific case?
  • using another variable (a naive test which obviously didn't work)

Compared to the other "questions" that might be seen as duplicate/similar ones to mine, I'm using print() on the function outside of it in the last example. So the problem is likely not coming from there.

I don't think my indentation is wrong here either.

To give even more details on "what doesn't work", when I use return instead of print here and using a test string like "ABABACD", it would output the correct "136" as a result. But when using return along with the other failed attempt I listed earlier, it would only output "1" instead....

ouflak
  • 2,458
  • 10
  • 44
  • 49
Nordine Lotfi
  • 463
  • 2
  • 5
  • 20
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [ask] from the [tour]. "Teach me this basic language feature" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a _specific_ question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – TigerhawkT3 Jan 17 '21 at 06:23
  • ...I never asked for "Teach me this basic language feature" and I'm well aware it's offtopic :D. I only explained my own specific problem, which wasn't the same as the duplicated link nor other post with similar title, although at first glance, it might seems like it is so. @TigerhawkT3 – Nordine Lotfi Jan 17 '21 at 06:25
  • I never believed that! I only saw Return used with the "+=" notation for increasing/incrementing a number once, and while i was always using return statements in my other functions, i noticed this didn't work here... @TigerhawkT3 – Nordine Lotfi Jan 17 '21 at 06:26
  • 1
    Yes, you did. `return` is a very fundamental language feature and you're asking for an explanation of its basic behavior. If the question is off topic, that means you _shouldn't post it_ - why would you? The duplicate is correct because it explains what `return` does and how it differs from `print`, as well as providing further tutorial resources. – TigerhawkT3 Jan 17 '21 at 06:28
  • If you see a keyword and don't understand what it does, you should look it up before asking a new question. Posting a new question is for when you have exhausted other resources - or, indeed, used any resources at all. You are expected to undertake some research effort before posting a question. – TigerhawkT3 Jan 17 '21 at 06:29
  • 1
    I did do those efforts...I explained i looked at other post, and your post(the one you linked) does indeed explain how it work, but it fail in doesn't really help in this example, since everything works beside that. @TigerhawkT3 – Nordine Lotfi Jan 17 '21 at 06:30

1 Answers1

5

Return ends the run of the function whether or not there are more "loops" left. If you want to have it return all of the positions there is an "A" in your string you can try something like this:

def position():
    return [i+1 for i, j in enumerate(string) if j == "A"]

same as

def position():
    pos = []
    for i, j in enumerate(string):
        if j == "A":
            pos.append(i+1)
    return pos

It can also be done using regular expressions:

import re

[i.start()+1 for i in re.finditer("A", string)]
goalie1998
  • 1,427
  • 1
  • 9
  • 16
  • Thanks :D. I'm curious though, why does it do that? And why did it not work when i tried as i mentioned to use += notation instead? – Nordine Lotfi Jan 17 '21 at 06:15
  • 1
    It's how return works. Return essentially breaks out of the function/loop and gives the "answer." If you try to put any code after a return statement, you'll see it won't run. I'm not sure what you meant by += above. But if you left return in the code, that's why. A return statement will execute once and cut the function off from continuing. – goalie1998 Jan 17 '21 at 06:21