i don't think you quite understand what the error is. Can you post the error message?
I suspect you trying to assign a0,hvt,at,lambd = signify(a0,hvt,at,lambd) is the real culprit, what if "rst" returning from the function does not have 4 elements? That syntax on the left hand side forces the list on the right hand side to be unpacked exactly into 4 elements, and raises a ValueError: too many values to unpack (expected 4) in a mismatch.
try result = signify(a0,hvt,at,lambd) and check output.
Update:
If you want to only modify only some of the 4 terms but allow the rest to pass as-is, you Just need an else portion. Here is how you can think of the entire process.
def signify(*args):
rst = []
print(args)
for arg in args:
if arg >= 100.0 or arg <= 0.01:
arg = '{:.2e}'.format(arg) #returns a string
rst.append(arg)
else:
rst.append(arg) #take note that this else statement is the same as the last statement of if block
#also note that args going through else block are not "strings" unlike the if block, which gives a string during ".format()"
return rst
We can improve on this.
def signify(*args):
rst = []
print(args)
for arg in args:
if arg >= 100.0 or arg <= 0.01:
arg = '{:.2e}'.format(arg)
rst.append(arg) #note that you may want to typecast to string to maintain uniformity.
#rst.append(str(arg))
return rst
However, This essentially is the same as applying a function on all args. We can create a function that emphasizes this "working on 1 term" approach.
def signify_single(single_arg):
if single_arg >= 100 or single_arg <= 0.01:
return '{:.2e}'.format(single_arg)
return single_arg #or str(single_arg)
a,b,c,d = (signify_single(x) for x in (101,202,303,40))
But that makes us realise this is just an if-else statement. They do not have to be ugly necessarily. (PS. That last line is a list comprehension.)
a,b,c,d = ('{:.2e}'.format(x)
if (x >= 100 or x <= 0.01)
else x #or str (x)
for x in (101,202,303,40))
The condition can be tweaked around a little to give us a cleaner comprehension.
Note that these can be written in a single line as well if you prefer.
a,b,c,d = (x if (0.01 < x < 100) else '{:.2e}'.format(x) for x in (101,202,303,40))
You can use any of the styles which looks the cleanest to you, Or explore and find something even better. Just apply it to your case like this.
a0,hvt,at,lambd = (x if (0.01 < x < 100) else '{:.2e}'.format(x) for x in (a0,hvt,at,lambd))