How to write a function that accepts a tuple that contains integer elements?

James Tan

Write a function average that accepts a tuple that contains integer elements as an argument and returns the average value of the elements within the argument.

for example, average((1, 2, 3)) = 2
for example, average((-3, 2, 8, -1)) = 1.5

my answer:

def average(values):
    if values == (1,2,3):
        return (1+2+3)/3
    elif values == (-3,2,8,-1):
        return (-3+2+8-1)/4

Why wrong? How to do? Thanks!!!

halex

Your function only works for those two specific inputs. The goal is to write a function that returns the correct average for all valid inputs.

You should use python's builtin functions sum and len for this

def average(values):
    return sum(values)/len(values)

For Python 2 you have to wrap the sum(values) (or the len(values), either part of the division is good) inside a call to float.

Starting with Python 3.4 you can use the function mean from the statistics module.

import statistics
def average(values):
    return statistics.mean(values)

or shorter as your function is just another name for statistic's mean

average = statistics.mean

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert elements(string) to integer in tuple in Python

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

How do I display an integer with a function that only accepts a char in C?

From Dev

How can ensure that a function only accepts a positive integer?

From Dev

how to write a (unix like) function that accepts pipe or file name input?

From Dev

How can I write a parser using Parsec that only accepts unique elements?

From Dev

Java: How to write generic function that accepts Enum constants that implement a given interface?

From Dev

How do I write a function that accepts Array<UInt8> or ArraySlice<UInt8>?

From Dev

How to write a function in T-SQL, which accepts a table as input and returns result back as a table?

From Dev

Why function literal with single tuple argument accepts a list of arguments as well?

From Dev

How to write constexpr swap function to change endianess of an integer?

From Dev

Write a function that returns a stack, that contains all elements smaller than a given number, and in same order?

From Dev

How to write a NectarJS program that accepts arguments?

From Dev

How to extend type that accepts generic elements of array?

From Dev

How to extend type that accepts generic elements of array?

From Dev

How to add elements individually in tuple?

From Dev

how to ignore the order of elements in a tuple

From Dev

how to add specific elements of a tuple

From Dev

Python Turtle: How to use the write function to join an integer and a string with the integer coming from a list

From Dev

Scala: How to write generic function on sortable collection with arithmetic elements

From Dev

How to write this function used in finding a series of elements in an array?

From Dev

Is it possible to write one template function that accepts both rvalue and lvalue

From Dev

Write template function that accepts mutable span as an immutable span

From Dev

Is it possible to apply a generic function over tuple elements?

From Dev

Create nested list of tuple elements based on the string integer number as index

From Dev

How to extract integer from list with tuple in Elixir?

From Dev

How do I convert a tuple to an Integer in python

From Dev

How to divide each element in a tuple by a single integer?

From Dev

Integer tuple to string tuple

Related Related

  1. 1

    How to convert elements(string) to integer in tuple in Python

  2. 2

    How can ensure that a function only accepts a positive integer?

  3. 3

    How do I display an integer with a function that only accepts a char in C?

  4. 4

    How can ensure that a function only accepts a positive integer?

  5. 5

    how to write a (unix like) function that accepts pipe or file name input?

  6. 6

    How can I write a parser using Parsec that only accepts unique elements?

  7. 7

    Java: How to write generic function that accepts Enum constants that implement a given interface?

  8. 8

    How do I write a function that accepts Array<UInt8> or ArraySlice<UInt8>?

  9. 9

    How to write a function in T-SQL, which accepts a table as input and returns result back as a table?

  10. 10

    Why function literal with single tuple argument accepts a list of arguments as well?

  11. 11

    How to write constexpr swap function to change endianess of an integer?

  12. 12

    Write a function that returns a stack, that contains all elements smaller than a given number, and in same order?

  13. 13

    How to write a NectarJS program that accepts arguments?

  14. 14

    How to extend type that accepts generic elements of array?

  15. 15

    How to extend type that accepts generic elements of array?

  16. 16

    How to add elements individually in tuple?

  17. 17

    how to ignore the order of elements in a tuple

  18. 18

    how to add specific elements of a tuple

  19. 19

    Python Turtle: How to use the write function to join an integer and a string with the integer coming from a list

  20. 20

    Scala: How to write generic function on sortable collection with arithmetic elements

  21. 21

    How to write this function used in finding a series of elements in an array?

  22. 22

    Is it possible to write one template function that accepts both rvalue and lvalue

  23. 23

    Write template function that accepts mutable span as an immutable span

  24. 24

    Is it possible to apply a generic function over tuple elements?

  25. 25

    Create nested list of tuple elements based on the string integer number as index

  26. 26

    How to extract integer from list with tuple in Elixir?

  27. 27

    How do I convert a tuple to an Integer in python

  28. 28

    How to divide each element in a tuple by a single integer?

  29. 29

    Integer tuple to string tuple

HotTag

Archive