TypeError: cannot unpack non-iterable NoneType object in Python
Dung Do Tien
Aug 20 2022
257
Hi everyone.
I have a question that needs anyone's helps to solve. I wrote a small Python application and I have a list of integers and want to average and find the max number of that array. Here is my code:
purchases = [30, 39, 480, 700, 760, 740, 800]
def calculate_statistics(purchases):
average_purchase = sum(purchases) / len(purchases)
largest_purchase = max(purchases)
average, largest = calculate_statistics(purchases)
print("The average purchase was ${}.".format(round(average, 2)))
print("The largest purchase was ${}.".format(round(largest, 2)))
But when ran this code I got an exception TypeError: cannot unpack non-iterable NoneType object. Here is detail of the exception:
Traceback (most recent call last):
File "main.py", line 7, in <module>
average, largest = calculate_statistics(purchases)
TypeError: cannot unpack non-iterable NoneType object
** Process exited - Return Code: 1 **
Press Enter to exit terminal
I installed Python 3.10
Anyone can explain and help me?
Thanks in advance.
Have 1 answer(s) found.
-
U0
Uad Dir Aug 20 2022
You can see the
calculate_statistics
function, you’ve forgotten a return statement:purchases = [30, 39, 480, 700, 760, 740, 800] def calculate_statistics(purchases): average_purchase = sum(purchases) / len(purchases) largest_purchase = max(purchases) return average_purchase, largest_purchase 'Need to return here average, largest = calculate_statistics(purchases) print("The average purchase was ${}.".format(round(average, 2))) print("The largest purchase was ${}.".format(round(largest, 2)))
#Output
The average purchase was $507.0. The largest purchase was $800. ** Process exited - Return Code: 0 ** Press Enter to exit terminal
Hope this is useful for you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.