+ 2

Is it the correct code for the following assignment ?

# I was trying to solve an assignment with Set in python. I was given a demo set and asked to write a single program that will calculate the sum of all numbers, the number of datum, and the average for the entire dataset. # here is my solution ``` numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10} def dataset(): total = 0 for item in numbers: total += item print("Average of dataset", total / len(numbers)) print("Number of datum", len(numbers)) return "Sum of all numbers", total print(dataset()) ``` # Output ('Average of dataset', 5.5) ('Number of datum', 10) ('Sum of all numbers', 55)

21st Dec 2018, 2:31 AM
🄼🄼🄷🄺
🄼🄼🄷🄺 - avatar
4 Respostas
+ 6
It works. You can just use sum() in place of the for loop. Here's a simple one: numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10} total = sum(numbers) n = len(numbers) print("Average of dataset", total / n) print("Number of datum", n) print("Sum of all numbers", total)
21st Dec 2018, 3:40 AM
David Ashton
David Ashton - avatar
+ 4
numbers = set(range(1, 11)) 😜
21st Dec 2018, 5:40 AM
Anna
Anna - avatar
+ 2
Thank you
31st Dec 2018, 6:44 AM
SoKovic ✌
SoKovic ✌ - avatar
+ 1
Thanks, David
21st Dec 2018, 3:45 AM
🄼🄼🄷🄺
🄼🄼🄷🄺 - avatar