Rhys Jones
Rhys Jones

Reputation: 53

Printing results of a function using a range of numbers

I am just learning python and I'm simply trying to print the results of a function using a range of numbers, but I am getting the error "The truth value of an array with more than one element is ambiguous."

print(t1) works and shows the range I want to use in the calculations.

print(some_function(55,t1)) produces the error

What am I missing?

Please note, I am doing this to help someone for an assignment and they can only use commands or functions that they have been shown, which is not a lot, basically just what's in the current code and arrays.

Thanks for any help

from pylab import *

def some_function(ff, dd):
    if dd >=0 and dd <=300:
        tt = (22/-90)*ff+24
    elif dd >=300 and dd <=1000:
        st = (22/-90)*(ff)+24
        gg = (st-2)/-800
        tt = gg*dd+(gg*-1000+2)
    else:
        tt = 2.0
    return tt

t1=arange(0,12000,1000)

print(t1)

print(some_function(55,t1))

Upvotes: 0

Views: 35

Answers (1)

run-out
run-out

Reputation: 3184

You are only making a minor error.

t1=arange(0,12000,1000)

print(t1)
[    0  1000  2000  3000  4000  5000  6000  7000  8000  9000 10000 11000]

You have to loop through t1 and call the function for each value in the loop.

for x in t1: 
    print(some_function(55,x))

10.555555555555555
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0

We are missing part of the loop in the calculation because of the values in t1. Let's adjust the range a bit.

t1=arange(0,2000,100)

print(t1)

[   0  100  200  300  400  500  600  700  800  900 1000 1100 1200 1300
 1400 1500 1600 1700 1800 1900]

And the resultant function:

for x in t1: 
    print(some_function(55,x))

10.555555555555555
10.555555555555555
10.555555555555555
10.555555555555555
8.416666666666668
7.347222222222222
6.277777777777779
5.208333333333334
4.138888888888889
3.0694444444444446
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0

Upvotes: 1

Related Questions