'How to use min() Function for floats
I am trying to find out the minimum value within a user define function.The values that are within the loop are float.But somehow I was unable to use min function for my conditional statement.My code is given below:
def life_age(x):
for i in range(1,16):
if 0<min(x[f'age_{i}'])<18:
return 1
Solution 1:[1]
It looks like x here is expected to look something like this:
x = {'age_1': 12.0, 'age_2':43.4, ..., 'age_16':90}
And you want to find out if the minimum age value is between 0 and 18.
The problem you are having is that min() is expecting an iterable of values like a list, but you are only passing it a single value.
So, you want to pass min() the entire list that you want it to find the minimum of:
def life_age(x):
ages = [x[f'age_{i}'] for i in range(1,16)]
if 0 < min(ages) < 18:
return True
else:
return False
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cargo23 |
