Search code examples
pythoninputmedian

Python adding median to user to odd user input


I'm trying to get the Median of the users given salary. Is it easy to implement it to the current code?

 def Lista():

    values = []
    print("Give your salary from smallest number to largerst.")
    while True:
        sum1 = int(input("Give your salary (0 ends): "))

        if sum1 == 0:
            print("Salary average:",sum(values)/len(values))
            print("Salary minimum:",min(values))
            print("Salary maximum:",max(values))
            print("Salary median:",values[len(values)/2])
            return False

        else:
            values.append(sum1)

    return Lista

Lista()

Can't seem to be able to do it myself. I'd use only odd numbers of asked salary


Solution

  • This is one way to get the solution integer the value and the length;

    int(values[int(len(values)/2)])
    

    Also I noticed in the average that it's the only one that ends in xxxx.0, this can be edited to be the same as others by editing it like this;

    int(sum(values)/len(values))
    

    So the full code would look like this

    def Lista():
    
        values = []
        print("Give your salary from smallest number to largerst.")
        while True:
            sum1 = int(input("Give your salary (0 ends): "))
    
            if sum1 == 0:
                print("Salary average:",int(sum(values)/len(values)))
                print("Salary minimum:",min(values))
                print("Salary maximum:",max(values))
                print("Salary median:",int(values[int(len(values)/2)]))
                return False
    
            else:
                values.append(sum1)
    
        return Lista
    
    Lista()
    

    Hopefully this helps :) E: sum were overlapping, changed it to sum1