-4

I'm defining a function by intervals. And when python compares the parameter fck to a delimiting value between 2 intervals (fck<50) it returns an error. I understand that it can't compare an array to an integer, but fck isn't an array! It's a parameter that I intend to be a float. How does python decide what type the parameter has and how can I define it?

In other similar functions, I've defined, everything's fine. I'm including the problematic function and an example of a working one. I can't see any differences between them. What is the problem? I don't only want to make it work, but I also want to understand what python is doing.

I've tried stating the parameter's type by doing def par_rec(eps,fck:float): and it doesn't change the outcome.

The definition that returns an error on it's second line:

def par_rec(eps,fck):
    if fck<50:
        eps0=0.002
    else:
        eps0=0.002+0.000085*(fck-50)**0.5
    if fck<50:
        epsu=0.0035
    else:
        epsu=0.0026+0.0144*((100-fck)/100)**4
    if fck<50:
        n=2
    else:
        n=1.4+9.6*((100-fck)/100)**4
    if eps<0:
        sig=0
    elif eps<eps0:
        sig=fck/1.5*(1-(1-eps/eps0)**n)
    else:
        sig=fck/1.5
    return sig

And a similar definition that doesn't cause any problems:

def fctm(fck):
    if fck<50:
        fctm = 0.3 * fck ** (2 / 3)
    else:
        fctm=0.58*fck**0.5
    return fctm

I'm defining the function, I'm not calling it, when I get an error:

line 81, in par_rec
    if fck<50:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
arsism
  • 1
  • 1
  • 2
    Please post the code you use to call the function. The question is what `fck` contains after you call the function – ksbg Jul 10 '19 at 11:16
  • 1
    `fck` must have _some_ value when you call `par_rec`, otherwise `par_rec` could not be called. Evidently that value is an array. – khelwood Jul 10 '19 at 11:16
  • Possible duplicate of [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Aprillion Jul 10 '19 at 11:18
  • It all depends on what `par_rec(eps,fck)` you call. `par_rec(5,10)` will take `fck`as an integer but `par_rec(5, '10')` will take it as a string. Have that in mind – M.K Jul 10 '19 at 11:18
  • "I've tried stating the variable's type by doing `fck:float` and it doesn't change the outcome." That is not the way to change a variable's type in Python... – Niels Henkens Jul 10 '19 at 11:32

2 Answers2

0

While you may think that your variable is not an array, this is not the case. In order to debug this, you need to print the type of your suspicious variable.

print(type(fck))
pygri
  • 647
  • 6
  • 17
0

but fck isn't an array!

fck is probably an array, because you do not provide any proof that it is not an array and you don't show us how you call your function. Please also show us print(type(fck)).

It's type wasn't defined anywhere

That is not possible in python. Every object (and a variable is also an object), has a type. If you do not assign any value, then variable will be undefined. If you assign a value, then python will automatically assign a matching type. If you want to initalize a valid but 'empty' variable, assign None.

Frieder
  • 1,208
  • 16
  • 25