0

I created the following function:

def print_basic_statistics(*numbers, output = ""):

    mean = (f"The mean of {numbers} is {statistics.mean(numbers):.1f}")
    median = (f"The median of {numbers} is {statistics.median(numbers):.1f}")
    standard_deviation = (f"The standard deviation of {numbers} is {statistics.stdev(numbers):.1f}")
    variance = (f"The variance of {numbers} is {statistics.variance(numbers):.1f}")

    if output == "mean":
        return mean
    elif output == "median":
        return median
    elif output == "sd":
        return standard_deviation
    elif output == "var":
        return variance
    else:
        return mean, median, standard_deviation, variance


print_basic_statistics(91, 82, 19, 13, 44)

Thus, if I don't specify "output" I want all 4 to be displayed. With the code above the result is:

Out[108]: 
('The mean of (91, 82, 19, 13, 44) is 49.8.',
 'The median of (91, 82, 19, 13, 44) is 44.0.',
 'The standard deviation of (91, 82, 19, 13, 44) is 35.6.',
 'The variance of (91, 82, 19, 13, 44) is 1267.7.')

I don't like the parantheses, apostrophes and commas.

I would like the result to look like this:

Out[108]: 
The mean of (91, 82, 19, 13, 44) is 49.8.
The median of (91, 82, 19, 13, 44) is 44.0.
The standard deviation of (91, 82, 19, 13, 44) is 35.6.
The variance of (91, 82, 19, 13, 44) is 1267.7.

Is there an easy way to do that?

I realize I can just write print(f"The mean of {numbers} is {statistics.mean(numbers):.1f}") instead of return mean, etc. But then I have to write each 'print' command twice. Once at if output == "mean" and once at else:.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
LizzyLou
  • 1
  • 2
  • 1
    Does this answer your question? [How to print a list of tuples with no brackets in Python](https://stackoverflow.com/questions/19112735/how-to-print-a-list-of-tuples-with-no-brackets-in-python) – mkrieger1 Apr 06 '23 at 13:42
  • 1
    If you don't want them formatted as a tuple, don't return them as a tuple. – Scott Hunter Apr 06 '23 at 13:43
  • Sorry, a better one is https://stackoverflow.com/questions/6167731/printing-list-elements-on-separate-lines-in-python – mkrieger1 Apr 06 '23 at 13:46
  • 1
    Thanks for the quick replies. I'm very new to this, I had to google what a tuple is. But I solved it by typing `return print(mean, median, standard_deviation, variance, sep = "\n")`. Annoyingly simple... – LizzyLou Apr 06 '23 at 14:01
  • And annoyingly wrong. Now your function returns the return value of the print function, which is None. Returning a tuple as in the original code was fine, you just need to print it like you have tried to *outside* the function: `result = print_basic_statistics(...)` and then `print(result, sep='\n')`. Or your function should *just* print something and *not* use `return` at the same time. – mkrieger1 Apr 06 '23 at 14:33
  • Ah okay, didn't know you shouldn't combine return & print, but what you're saying makes sense, thanks! – LizzyLou Apr 12 '23 at 11:32

1 Answers1

0

You could return the values as a concatenated string with escape characters:

return (mean + '\n' + median + '\n' + standard_deviation + '\n' + variance + '\n')

A more efficient solution however is to add the escape characters to the end of each variable declaration, and just return the concatenated strings:

def print_basic_statistics(*numbers, output = ""):

    mean = (f"The mean of {numbers} is {statistics.mean(numbers):.1f}\n")
    median = (f"The median of {numbers} is {statistics.median(numbers):.1f}\n")
    standard_deviation = (f"The standard deviation of {numbers} is {statistics.stdev(numbers):.1f}\n")
    variance = (f"The variance of {numbers} is {statistics.variance(numbers):.1f}\n")

    if output == "mean":
        return mean
    elif output == "median":
        return median
    elif output == "sd":
        return standard_deviation
    elif output == "var":
        return variance
    else:
        return mean + median + standard_deviation + variance
LWB
  • 426
  • 1
  • 4
  • 16
  • This sounds like a great solution, but it doesn't seem to work for me. The result is now `Out[133]: 'The mean of (91, 82, 19, 13, 44) is 49.8\nThe median of (91, 82, 19, 13, 44) is 44.0\nThe standard deviation of (91, 82, 19, 13, 44) is 35.6\nThe variance of (91, 82, 19, 13, 44) is 1267.7\n'` in one line. – LizzyLou Apr 06 '23 at 14:09
  • @LizzyLou When I run this it prints it out fine. I suppose you may be using something to print out your result that doesn't account for escape characters in a string? Anyway you have an even better solution than mine now. – LWB Apr 06 '23 at 14:17
  • 1
    The `\n` characters are only displayed as line breaks when using the `print()` function with the returned string. – mkrieger1 Apr 06 '23 at 14:38