Search code examples
pythonpython-3.xmedian

How can I calculate the median of a range of numbers that I input?


I want to calculate the median of a range of numbers I input. The range should be between 1 and n+1 (n is the input in my case). I made some research and saw that you can use the inbuild statistics.median. But I can't make it work.

import.statistics
n = int(input("Please input your number: "))
range_1 = range(1, n+1)
list_1 = list(range_1)

I created a list with all values between 1 and n+1 and now I want to calculate the median.

print(statistics.median(list_1))

This is my error I don't know how to print the median. What am I doing wrong? I can feel that it's really simple. Thank you for your help.


Solution

  • Do it like this.

    import statistics
    
    number = int(input("enter the number :"))
    range1 = list(range(1, number+1))
    median1 = statistics.median(range1)
    print("Median of the given range is :", median1)