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....