-1

I'm trying to make a function using *args. It should take in certain numbers and then output the products of the numbers multiplied by 10.

I'm learning to use *args but I'm having trouble getting the output. My result only outputs the first number in the function. I've tried using a for loop, thinking it would iterate over each character, but it didn't work.

def myfunc(*args):
for args1 in args:
    return args1*10

When I run the function:

myfunc(10.1,20)

The expected output should be

(101.0,200.0)

The actual output is

(101.0)

Any help would be appreciated. Thanks.

2 Answers2

1

If I understand you, that's what you need:

def myfunc(*args): 
    return tuple(map(lambda x: x*10, args))

*args can be retrieved in the function as a list called args. What you do here is just multiplying the first element by 10 and return it, so the function stops.

Ludal
  • 110
  • 1
  • 7
0

You should use yield instead of return, to make your function return a generator, which can then be converted to tuple whenever you need

>>> def myfunc(*args):
...     for args1 in args:
...         yield args1*10
... 
>>> 
>>> myfunc(10.1,20)
<generator object myfunc at 0x7f20d1ae8ad0>
>>> 
>>> tuple(myfunc(10.1,20))
(101.0, 200)
Sunitha
  • 11,777
  • 2
  • 20
  • 23