I am having a problem using *args in a function I am defining. Here is an example of one code:
def feet_to_meter(*args):
"""
:param *args: numeric values in feet
:output: returns list of values in meter
"""
value_list = [ ]
conversion_factor = 0.3048
for arg in args:
try:
value_list.append(arg * conversion_factor)
except TypeError:
print(str(arg) + "is not a number")
return value_list
print ("Function call with 3 values: ")
print(feet_to_meter(3, 1, 10))
I understand that the code should convert the 3 values but somehow after executing the program I only get the result of the first value. I am using Pydroid 3.
Thanks in advance
The return value_list
needs to have one less tab. Working version below.
def feet_to_meter(*args):
"""
:param *args: numeric values in feet
:output: returns list of values in meter
"""
value_list = [ ]
conversion_factor = 0.3048
for arg in args:
try:
value_list.append(arg * conversion_factor)
except TypeError:
print(str(arg) + "is not a number")
return value_list
This happens because the return
statement is executed on every iteration of the for
, but a return will immediately exit the function after only one append has occurred. Placing the return
at the same indentation as the for
allows the for
to finish executing before finally returning value_list