15,916,462 members
See more:
I keep getting this error in my code and I don't know how to fix it:
```line 27, in Northeast_Analysis
light_count = np.count_nonzero((5,  northeast_packages) & (northeast_packages,  50))
TypeError: unsupported operand type(s) for &: 'tuple' and 'tuple'
>>> c```

This is my code so far:

```# This program analyzes We-Truck-It package delivery information and
# generates truck purchase type recommendations for each of the
# We-Truck-It company U.S. Delivery regions.

#import the numpy.py library for random number generation and data analysis

import numpy as np

# store the number of packages delivered in the northeast and northwest
# in the variables ne_num and nw_num

ne_num = 60831
nw_num = 50629

def Northeast_Analysis():

# Recommend the best truck purchase type for the Northeast Region

# use the numpy.py random number function to simulate
# package weights for the Northeast

northeast_packages = np.random.uniform(low=5.0, high=150, size=(ne_num,))

# use numpy.py to determine the number of packages within the recommended
# ranges for the light, medium and heavy truck types

light_count = np.count_nonzero((5,  northeast_packages) & (northeast_packages,  50))
medium_count = np.count_nonzero((51,  northeast_packages) & (northeast_packages,  100))
heavy_count = np.count_nonzero((101,  northeast_packages) & (northeast_packages,  150))

# print the number of trucks within the recommended package delivery weight
# ranges for light, medium and heavy trucks
# print values with two decimal points

print (' ')
print (' *** Northeast truck purchase analysis')
print (' ')
print ('number of Northeast packages in the light truck weight range:')
print (format(light_count, '.2f'))

print ('number of Northeast packages in the medium truck weight range:')
print (format(medium_count, '.2f'))

print ('number of Northeast packages in the heavy truck weight range:')
print (format(heavy_count, '.2f'))

# Generate a truck purchase recommendation based on the most common package weight

if   (light_count,  medium_count) and (light_count,  heavy_count):
message = ('The light truck is the best purchase for the Northeast region')
elif (medium_count,  light_count) and (medium_count,  heavy_count):
message = ('The medium truck is the best purchase for the Northeast region')
else:
message = ('The heavy truck is the best purchase for the Northeast region')

print (' ')

print (message)

def Northwest_Analysis():

# Recommend the best truck purchase type for the Northwest Region

# use the numpy.py random number function to simulate
# package weights for the Northwest

northwest_packages = np.random.uniform(low=5.0, high=150, size=(nw_num,))

# use numpy.py to determine the number of packages within the recommended
# ranges for the light, medium and heavy truck types

light_count = np.count_nonzero((5,  northwest_packages) & (northwest_packages,  50))
medium_count = np.count_nonzero((51,  northwest_packages) & (northwest_packages,  100))
heavy_count = np.count_nonzero((101,  northwest_packages) & (northwest_packages,  150))

# print the number of trucks within the recommended package delivery weight
# ranges for light, medium and heavy trucks
# print values with two decimal points
print (' ')
print (' *** Northwest truck purchase analysis')
print (' ')

print ('number of Northwest packages in the light truck weight range:')
print (format(light_count, '.2f'))
print ('number of Northwest packages in the medium truck weight range:')
print (format(medium_count, '.2f'))

print ('number of Northwest packages in the heavy truck weight range:')
print (format(heavy_count, '.2f'))

# Generate a truck purchase recommendation for the most common package weight

if (light_count,  medium_count) and (light_count,  heavy_count):
message = ('The light truck is the best purchase for the Northwest region')
elif (medium_count,  light_count) and (medium_count,  heavy_count):
message = ('The medium truck is the best purchase for the Northwest region')
else:
message = ('The heavy truck is the best purchase for the Northwest region')

print (' ')
print (message)

# Call the functions to analyze the Northeast and Northwest delivery information

Northeast_Analysis()
Northwest_Analysis()
```

What I have tried:

I have tried to fix the errors but Im stuck. In the long run I am supposed to:

Create a function similar to the Northeast_Analysis() and Northwest_Analysis () functions to determine the recommended truck purchase type for the Southeast region. Assume this region delivers 44,000 packages in a year.

Create a se_num variable to hold the number of packages shipped by the Southeast region.

Use the numpy.py random number function to simulate package weights for the 44,000 packages shipped in the Northeast.

Use the np.random.uniform(low=5.0, high=150, size=(se_num,)) to randomly generate the 44,000 packages using a minimum package weight of 5.0 and maximum package weight of 150 pounds.

Use the np.count function to count the number of packages for the recommended package delivery weight for light, medium and heavy trucks.

Print out the number of packages that fall within the weight ranges for each delivery truck.

Print the recommended truck type to purchase for the Southeast.
Posted

## Solution 1

Your use of `&` is trying to perform a bitwise operation, which is not supported on tuples. What you seem to be looking for is the `and` keyword. Replace `&` with `and`.

Elizabeth farriell 18-Apr-24 10:31am
When I replace & with and I get the error:

/usr/local/lib/python3.10/site-packages/numpy/core/numeric.py:486: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecat
ed. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
return multiarray.count_nonzero(a)
Traceback (most recent call last):
File "/home/ElizabethFarriell/truckit.py", line 106, in <module>
Northeast_Analysis()
File "/home/ElizabethFarriell/truckit.py", line 27, in Northeast_Analysis
light_count = np.count_nonzero((5, northeast_packages) and (northeast_packages, 50))
File "<__array_function__ internals>", line 5, in count_nonzero
File "/usr/local/lib/python3.10/site-packages/numpy/core/numeric.py", line 486, in count_nonzero
return multiarray.count_nonzero(a)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>>
Pete O'Hanlon 18-Apr-24 10:48am
So numpy has deprecated this. It's possible that will still work, just add the following:

import warnings
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 85 Richard Deeming 60 Dave Kreskowiak 55 Pete O'Hanlon 20 Richard MacCutchan 10
 Pete O'Hanlon 690 OriginalGriff 373 Dave Kreskowiak 370 Richard MacCutchan 300 merano99 215

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900