0

I was given this question and I'm not sure why I'm getting an error with my code.

Question 4. Suppose there were 30,000 beachgoers in 1930 and the number of beachgoers increased by 15% year-after-year. Create an array called beachgoers that contains the number of beachgoers in each year between 1930 and 2017. If any beachgoer is equally likely to be attacked by a shark, how dangerous was the least dangerous year for shark attacks? Assign the percent chance of being attacked by a shark to the variable danger. Hint: To calculate beachgoers, you may find the function np.arange helpful.

yearly_growth_rate =  30000 * 1.15 ** np.arange(1, 88) - 30000
final_number = 30000 * (1.15 ** 88)
beachgoers = np.arange(30000, final_number, yearly_growth_rate)
beachgoers
#danger = ...

This is the error I'm getting

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-df37b4a34579> in <module>()
      1 yearly_growth_rate =  30000 * 1.15 ** np.arange(1, 88) - 30000
      2 final_number = 30000 * (1.15 ** 88)
----> 3 beachgoers = np.arange(30000, final_number, yearly_growth_rate)
      4 beachgoers
      5 #danger = ...

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Thanks for the help in advance

  • I think this answers the question, right? https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – Zevgon Oct 14 '18 at 21:23
  • I can't see where I'm evaluating the array in a boolean context though – David Dempsey Oct 14 '18 at 21:32

3 Answers3

0

I think this is because your yearly_growth_rate is an array, rather than an int. The numpy.arange() function takes four params:

start = Optional int type stop = Required place to stop step-size = Optional size between elements (int) dtype = optional output type of arange

Check the docs for help

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
0

The third parameter for np.arange is a number and you're providing an array. It's possible that the implementation of np.arrange uses the truth value of the parameter to check that it's not either None or zero.

Tim
  • 9,171
  • 33
  • 51
0

You need just

30000 * (1.15 ** np.arange(0, 88))

which will translate to

[30000 * (1.15 ** 0),
 30000 * (1.15 ** 1),
 30000 * (1.15 ** 2),
 ...]

The last argument of np.arange is the step, which should be a number. It makes no sense to pass an array as argument.

rafaelc
  • 57,686
  • 15
  • 58
  • 82