-3

i have a code which looks like this

def monokode(b):
while f == 0:
    if g[h]== b[i]:
        i = i + 1
        j = h - e
        if j >= 100:
            j = (e-h + len(g))*-1
        elif j <= -100:
            j = (e-h - len(g))*-1
        if j < 10 and j >=0:
            print 0,0,j,
        elif j < 0:
            j = j*-1
            if j < 10:
                print 1,0,j,
            elif j >= 10 and j < 100:
                print 1,j,
            else:
                print 1,j,
        elif j >= 10 and j < 100:
            print 0,j,
        else:
            print 0,j,
        h = 0
        if i >= len(b):
            f = 1
    else:
        h=h+1

now, i want to make all the statements that print right now, return their value instead. but after some tries, i've realised that i don't know enough about the return statement to use it properly. how can i make the program return multiple values in a long line and then print them afterwards?

  • See my answer here to a *very* similar problem http://stackoverflow.com/questions/22840090/how-to-return-multiple-values-in-python/22840180#22840180 – msvalkon Apr 03 '14 at 21:24
  • 2
    Also a word of advice, please use meaningful variable names. What are `f, b, g, i, j, h` supposed to mean? – msvalkon Apr 03 '14 at 21:26
  • Does your script work? I am wondering that python should accept the comma at the end of a print statement. However, you can use tuples to return multiple values. Example: `return (0, 0, j)`, which you can then store in some variables `a,b,c = myFunc()` or into a single variable `a = myFunc()`. If you use a single variable, you can access the values using the [] operator. – Phidelux Apr 03 '14 at 21:28

4 Answers4

1

UPDATED ANSWER:

Okay as I understand the question you're wanting to make your function return multiple variables and then print them?

I agree with most of the other replies. Store the objects you want to print in a list then call

print ', '.join(list)

right before you return whatever values from the program:

like this:

def monokode(b):
    printspool=[]
    while f == 0:
        if g[h]== b[i]:
            i = i + 1
            j = h - e
            if j >= 100:
                j = (e-h + len(g))*-1
            elif j <= -100:
                j = (e-h - len(g))*-1
            if j < 10 and j >=0:
                printspool.append([0,0,j])
            elif j < 0:
                j = j*-1
                if j < 10:
                    printspool.append([1,0,j])
                elif j >= 10 and j < 100:
                    printspool.append([1,j])
                else:
                    printspool.append([1,j])
            elif j >= 10 and j < 100:
                printspool.append([0,j])
            else:
                printspool.append([0,j])
            h = 0
            if i >= len(b):
                f = 1
        else:
            h=h+1

    #Now here I'll print out everything contained in the printspooler list
    for node in printspooler:
        print ','.join(node)

    #This is where you would 
    return Whatevervariable, Whatevervariable2, Whatevervariable3 etc...
Amazingred
  • 1,007
  • 6
  • 14
0

Collect your values into a list instead, then return that list:

def yourfunction(your_arguments):
    results = []

    while f == 0:
        if g[h]== b[i]:
            i = i + 1
            j = h - e
            if j >= 100:
                j = (e-h + len(g))*-1
            elif j <= -100:
                j = (e-h - len(g))*-1
            if j < 10 and j >=0:
                results.extend([0,0,j])
            elif j < 0:
                j = j*-1
                if j < 10:
                    results.extend([1,0,j])
                elif j >= 10 and j < 100:
                    results.extend([1,j])
                else:
                    results.extend([1,j])
            elif j >= 10 and j < 100:
                results.extend([0,j])
            else:
                results.extend([0,j])
            h = 0
            if i >= len(b):
                f = 1
        else:
            h=h+1

    return results
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

You can use return and say many variable names...

return a, b, c

and what is returned is a tuple in the same order.. eg. return 1, 2, 3

will be (1, 2, 3)

and you can directly print call_fn() # if this returns those values

adarsh
  • 6,738
  • 4
  • 30
  • 52
0

From what I understand, you can to store the values into a list:

def myfunc(…)
    …
    myList=[]
    while f == 0:
        if g[h]== b[i]:
            i = i + 1
            j = h - e
            if j >= 100:
                j = (e-h + len(g))*-1
            elif j <= -100:
                j = (e-h - len(g))*-1
            if j < 10 and j >=0:
                myList.extend([0,0,j])
            elif j < 0:
                j = j*-1
                if j < 10:
                    myList.extend([1,0,j])
                elif j >= 10 and j < 100: #this test
                    myList.extend([1,j])
                else:
                    myList.extend([1,j])
            elif j >= 10 and j < 100: #and this one
                myList.extend([0,j])
            else:
                myList.extend([0,j])
            h = 0
            if i >= len(b):
                f = 1
        else:
            h=h+1
    return myList

and then print myFunc(…), or maybe something like

results = myFunc(…)
for r in results:
    print r,

Edit: I've marked 2 tests that I think are unnecessary, since in the èlse part, you print the same thing

fredtantini
  • 15,966
  • 8
  • 49
  • 55