0

When I run the code below, I get back only the first result. If I replace return (a,d) with print (a,d), I get the full set of results. (I understand that running print (a,d) doesn't save the output anywhere.)

What do I need to change to get the full output and not just the first result?

nums = [(str(i)) for i in range(100,106)]

def foo(aa):
    for a in nums:
        for b in a :
            c= sum(int(b)**2 for b in a)
            d=''.join(sorted(a,reverse=True))
            if (c>5):
                return(a,d) 


output = foo(nums)
print(output)

UPDATE -- I'm expecting the following output:

103 310
103 310
103 310
104 410
104 410
104 410
105 510
105 510
105 510

The return(a,d) gives me just:

103 310
egon
  • 949
  • 2
  • 7
  • 12
  • `return (a, d)` returns a tuple with two values. It will definitely capture both values. But `print(output)` will not generate the same output as `print(a, d)` – poke Oct 23 '18 at 17:15
  • https://repl.it/@codeguru/VillainousUniqueServers As far as I can tell, this prints "the full set of results". If you run the code in the above link, you get the output `('103', '310')` which looks like both results to me, not just the first one. – Code-Apprentice Oct 23 '18 at 17:20
  • 1
    While the questioner is returning multiple values, the question text indicates that they want to return even more; they want to return every value that would be printed if `return` was replaced by `print`. That needs a "use a list/generator" dupe, not a tuple dupe. – user2357112 Oct 23 '18 at 17:21

1 Answers1

1

Here you are

nums = [(str(i)) for i in range(100,106)]

def foo(aa):
    for a in nums:
        for b in a :
            c= sum(int(b)**2 for b in a)
            d=''.join(sorted(a,reverse=True))
            if (c>5):
                return(a,d)


output , output2 = foo(nums)
print(output, output2)

EDIT

create a list and insert tuples

nums = [(str(i)) for i in range(100,106)]

def foo(aa):
    list_of_numbs = list() # create a list
    for a in nums:
        for b in a :
            c= sum(int(b)**2 for b in a)
            d=''.join(sorted(a,reverse=True))
            if (c>5):
                list_of_numbs.append((a,d)) #insert your desire tuplet in the list
                #print(a,d)
    return list_of_numbs # return the list

x = foo(nums)
print(x) # print the list
# OR
for i,j in x:     
    print(i,j)
Anagnostou John
  • 498
  • 5
  • 14
  • Please explain in words a little more about how this solves the problem. – Code-Apprentice Oct 23 '18 at 17:19
  • This worked, thank you! The main thing I had overlooked was putting return at the indented level. – egon Oct 23 '18 at 18:13
  • 1
    If you just want to print out a tuple normally without having to write out variables, you can just do this: `value = function()` where function can return any a tuple that could be any size, then you print such tuple like `print(*value)`, it's called tuple unpacking and if `value` is `(a, b, c)`, then `print(*value)` is equivalent to `print(a, b, c)`. – Purple Ice Oct 23 '18 at 21:32