1

I am new to python and trying to make function that will calculate the average value of inputs,how can I modify my function so it will return float to the nearest quarter 0.25 i.e the output can only be x.0 x.25 x.50 x.75, I wrote the following function but I am not sure how to go next

def calculate_average(*args):
    total = sum(args)
    avg = total/len(args)
    return avg

example of output

#calculate_average(30,35,15) -> 26.25
#calculate_average(20,28,14) -> 20.75
#calculate_average(1,2,3) -> 2.0

1 Answers1

1

you need to create a custom function to round to nearest quarter.

In [34]: def quarter_round(x):
    ...:     print(round(x*4)/4)
    ...:

In [35]: def calculate_average(*args):
    ...:     total = sum(args)
    ...:     avg = total/len(args)
    ...:     return avg
    ...:

In [36]: quarter_round(calculate_average(4,9,3))
5.25

In [37]:
Rajat Mishra
  • 3,635
  • 4
  • 27
  • 41