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_listneeds to have one less tab. Working version below.This happens because the
returnstatement is executed on every iteration of thefor, but a return will immediately exit the function after only one append has occurred. Placing thereturnat the same indentation as theforallows theforto finish executing before finally returningvalue_list