I'm trying to write a Python program using numpy, which prints the average/mean of all the even numbers bigger than 10 which are also between a specific lower and upper bound input by the user. So, if the user inputs 8 as the lower number and 16 as the upper number, then the output would be 14, but I can't seem to get it.
This is what I tried so far.
import numpy as np
lower = int(float(input('Waiting for input: ')))
upper = int(float(input('Waiting for input: ')))
def sum_even(a, b):
count = 0
for i in range(a, b, 1):
if(i % 2 == 0):
count = i
return count
print(f"{function1(lower, upper):.2f}")
But it's not giving me the average.
CodePudding user response:
Your function is just adding all of the even numbers between the lower and upper bound (excluding the upper bound because of how the range function works).
So when you input 8 and 16, you're computing 8 10 12 14=44.
You need to keep track of how many evens there are in the function and then divide the sum by that value, as such:
import numpy as np
lower = int(float(input('Waiting for input: ')))
upper = int(float(input('Waiting for input: ')))
def mean_even(a, b):
count = 0
sums = 0
for i in range(a, b 1, 1):
if(i % 2 == 0) & (i>10):
count = 1
sums = i
return sums/count
print(f"{mean_even(lower, upper):.2f}")
Then, using your example inputs:
>> Waiting for input: 8
>> Waiting for input: 16
14.00
CodePudding user response:
try this:
import numpy as np
lower = int(float(input('Waiting for input: ')))
upper = int(float(input('Waiting for input: ')))
def sum_even(a, b):
count = 0
n = 0
for i in range(a, b 1, 1):
if(i % 2 == 0):
count = i
n = 1
return count/n
print(f"{sum_even(lower, upper):.2f}")
since you want to get the average you have to divide the count by n
CodePudding user response:
So far, the answers you have don't use Numpy, which was a stated goal in the question, and they are inefficient because they use a step of 1 in the range
function and check every value to see if it is even. If you ensure that the starting value for the range is a) greater than 10 and b) even, you don't need to check any of the values at all - you can just use a range function with a step of 2.
As you stated that you wanted to use Numpy, this just creates an array of the range of valid numbers and uses the Numpy mean()
function to return the mean.
import numpy as np
lower = int(float(input('Waiting for input: ')))
upper = int(float(input('Waiting for input: ')))
def mean_even(a, b):
if a < 11:
a = 12
elif a % 2 != 0:
a = 1
return np.array(range(a, b 1, 2)).mean()
print(f"{mean_even(lower, upper):.2f}")
Even if you don't want to use Numpy, you can still improve the efficiency by scrapping the for loop and the conditionals:
lower = int(float(input('Waiting for input: ')))
upper = int(float(input('Waiting for input: ')))
def mean_even(a, b):
if a < 11:
a = 12
elif a % 2 != 0:
a = 1
valid_vals = list(range(a, b 1, 2))
return sum(valid_vals) / len(valid_vals) if len(valid_vals) > 0 else 0
print(f"{mean_even(lower, upper):.2f}")
Note that the conditional is there to prevent the return from failing of there are no valid values in the list.