Find the mean of the numbers in the list (using for statements)

Asked 2 years ago, Updated 2 years ago, 152 views

You want to average the prices in the fruit list! I even extracted the price from the list I think I can use the sum, but it's stuck.

fruit = ["Apple", 1020] ["Orange", 880] ["Grape", 3160]

sum=0

for i in fruit:
    print(i[1])
    #sum+=i

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

It pops up like this. What should I do?

list for average sum

2022-09-20 16:22

1 Answers

I understand that the average is usually calculated like this.

fruit = ["Apple", 1020] ["Orange", 880] ["Grape", 3160]
num = [i[1] for i in fruit]
plus = sum(num)
average = plus / len(num)
print(average)

>> 1686.6666666666667


2022-09-20 16:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.