How to fix cannot unpack non-iterable int object?

Last Updated on June 22, 2021 by admin

wondering today I will fix cannot unpack non-iterable int object Python sequence can be unpacked.

fix cannot unpack non-iterable int object

This implies you can dole out the substance of an arrangement to different factors.

In the event that you attempt to unload a None worth utilizing this sentence structure, you’ll experience the “TypeError: can’t unload non-iterable NoneType object” error.

Explanation

Unpacking syntax allows you to allocate numerous factors simultaneously dependent on the substance of a succession.

Think about the accompanying code:

1 Fruit_sales = [230, 310, 219]

2 Avocado, bananas, apples = fruit_sales

This code allows us to dole out the qualities in the fruit_sales variable to three separate factors.

The worth of “avocado” becomes 230, the worth of “bananas” gets 310, and the worth of “apples” gets 219.

The unloading language structure just deals with successions, similar to records and tuples.

You can’t unload a None worth since None qualities are not successions.

This mistake is ordinarily raised in the event that you attempt to unload the consequence of a capacity that does exclude a bring proclamation back.

Example:

We will make a program that ascertains the normal price tag at a bistro and the biggest buy made on a given day.

Start by characterizing a rundown of buys made on a specific day:

1 Buys = [2.30, 3.90, 4.60, 7.80, 2.20, 2.40, 8.30]

Then, characterize a capacity.

This capacity will compute the normal price tag and the biggest buy made on a given day:


def calculate_statistics(purchases):

average_purchase = sum(purchases)/len(purchases)
largest_purchase = max(purchases)

To ascertain the normal worth of a buy, partition the absolute worth of all buys by the number of buys are made on a given day.

We utilize the maximum() capacity to track down the biggest buy.

Since we’ve characterized our capacity, we can call it.

We relegate the qualities our capacity gets back to factors:

1 Normal, biggest = calculate_statistics(purchases)

We utilize the unloading language structure to get to these two qualities from our capacity.

This code allows us to get to the qualities our capacity returns in our principle program.

Since we approach these qualities outside our capacity, we print them to the control center:


print(“The normal buy was ${}.”.format(round(average, 2)))

print(“The biggest buy was ${}.”.format(round(largest, 2)))

Our code will show us the normal buy esteem and the worth of the biggest buy on the control center.

We round both of these qualities to two decimal spots utilizing the round() strategy.

We should run our program and see what occurs:


traceback (most recent call last):

file “main.py”, line 7, in <module>

average, largest = calculate_statistics(purchases)

TypeError: cannot unpack non-iterable NoneType object

Our code returns an error message.

Solution

The error happens on the line where we attempt to unload the qualities from our capacity.

Our Error message reveals to us we’re attempting to unload values from a None worth.

This discloses to us our capacity isn’t returning the right qualities.

In the event that we investigate our capacity, we see we’ve failed to remember a bring proclamation back.

This implies our code can’t unload any qualities.

To settle this mistake, we should return the “average_purchase” and “largest_purchase” values in our capacity so we can unload them in our principle program:

def calculate_statistics(purchases):


average_purchase = sum(purchases) / len(purchases)

largest_purchase = max(purchases)

return average_purchase,

Let’s run our code

The average purchase was $4.5.

The largest purchase was $8.3.

Our code tells us the average purchase was $4.50 and the largest purchase was $8.30.

Read More: How to comment out multiple lines in python?

Leave a Comment