Click here to Skip to main content
15,924,196 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have to make the following working by using conditionals

# 1. arithmetic mean (INPUT:LIST)

def calc_mean(mylist):

# 2. harmonic_mean (INPUT:LIST)
def calc_harmonic_mean(mylist):

# 3. median (INPUT:LIST)

def calc_median(mylist):

# 4. low median (INPUT:LIST)
def calc_low_median(mylist):

# 5. high median (INPUT:LIST)
def calc_high_median(mylist):

# 6. mean(INPUT:DICTIONARY)
def calc_mean_dicts(mydict):



What I have tried:

I tried the following, but it didn't work. Is it a problem on the code syntax or the order of the code orders? How would look like otherwise?

Python
import statistics
import numpy as np

def calc_mean(mylist):
    mylist=[1, 3, 4, 5, 7, 9, 2]
    x = statistics.calc_mean(mylist)
    print("Mean is :", x)

if __name__== '__main__':
    calc_mean()


def calc_harmonic_mean(mylist):
     mylist=[1, 3, 5, 7, 9]
     print("Harmonic Mean is % s " % (statistics.calc_harmonic_mean(mylist)))

if __name__== '__main__':
    calc_harmonic_mean()



def calc_median(mylist):
        mylist = [20, 2, 7, 1, 34]
        if mylist>8:
            print("arr : ", mylist) 
            print("median of arr : ", np.calc_median(mylist))

if __name__== '__main__':
    calc_median()
 
def calc_low_median(mylist):
    mylist = [1, 3, 3, 4, 5, 7]
    print("Low median of the data-set is % s " % (statistics.calc_low_median(mylist)))

if __name__== '__main__':
    calc_low_median()

def calc_high_median(mylist):
    mylist = [1, 3, 2, 8, 5, 4]
    print("High median of the data-set is %s "% (statistics.calc_high_median(mylist)))

if __name__== '__main__':
    calc_high_median()    


def calc_mean_dicts(mydict):
    mydict = {"Gfg" : 4, "is" : 7, "Best" : 8, "for" : 6, "Geeks" : 10}
    print("The original dictionary is : " + str(mydict))

if __name__== '__main__':
    calc_mean_dicts()
Posted
Updated 13-Dec-21 22:28pm
Comments
CHill60 13-Dec-21 14:14pm    
What do you mean by "didn't work" - was there an error message? Was the value incorrect?
Herme97 13-Dec-21 14:21pm    
It doesn't show any results, except the source of the file "f:/desktop...."

Does the order and the code in general look correct?
0x01AA 13-Dec-21 15:04pm    
Simply googling for 'python ImportError: No module named statistics' shows me
linux - Python ImportError no module named statistics after downloading - Stack Overflow[^]
Hope it helps ;)

1 solution

You have multiple entries of the form:
Python
if __name__== '__main__':
    // call to a function

You should remove all except one at the bottom of the file followed by calls to all the other functions in order. So:
Python
import statistics
import numpy as np

def calc_mean(mylist):
    mylist=[1, 3, 4, 5, 7, 9, 2]
    x = statistics.calc_mean(mylist)
    print("Mean is :", x)

def calc_harmonic_mean(mylist):
     mylist=[1, 3, 5, 7, 9]
     print("Harmonic Mean is % s " % (statistics.calc_harmonic_mean(mylist)))

def calc_median(mylist):
        mylist = [20, 2, 7, 1, 34]
        if mylist>8:
            print("arr : ", mylist) 
            print("median of arr : ", np.calc_median(mylist))

 
def calc_low_median(mylist):
    mylist = [1, 3, 3, 4, 5, 7]
    print("Low median of the data-set is % s " % (statistics.calc_low_median(mylist)))


def calc_high_median(mylist):
    mylist = [1, 3, 2, 8, 5, 4]
    print("High median of the data-set is %s "% (statistics.calc_high_median(mylist)))


def calc_mean_dicts(mydict):
    mydict = {"Gfg" : 4, "is" : 7, "Best" : 8, "for" : 6, "Geeks" : 10}
    print("The original dictionary is : " + str(mydict))

if __name__== '__main__':
    calc_mean()
    calc_harmonic_mean()
    calc_median()
    calc_low_median()
    calc_high_median()    
    calc_mean_dicts()

You also need to fix all the function calls to pass the required parameter that each function expects.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900