Python: I am having trouble figuring out how to display a list of numbers that occur above the calculated average from a user-given list and any from that list that occurs over 90. this is what I have been working on so far. The average calculation works ok alone, its the addition of these two functions I can't work out.
from math import sqrt
def getNumbers():
nums = []
xStr = input("Enter a number (<Enter> to quit) >> ")
while xStr != "":
x = eval(xStr)
nums.append(x)
xStr = input("Enter a number (<Enter> to quit) >> ")
return nums
def mean(nums):
sum = 0.0
for num in nums:
sum = sum + num
return sum / len(nums)
def grt_mean(mean):
grt_mean_ls = nums []
for num in nums:
if num > mean:
grt_mean_ls = nums
nums.append(grt_mean_ls)
def above90(nums):
above_list = nums[]
for num in nums:
if num >90:
above_list = nums
nums.append(above_list)
def display(nums):
print(nums)
def main():
abmean = grt_mean(mean)
ab90 = above(nums)
data = getNumbers()
xbar = mean(data)
display(data)
print("\nThe mean is", xbar)
print("The numbers above 90 are: ab90")
print("the numbers above the average are:")
main()
You could use a list comprehension to check each value against your mean
def mean(nums):
if len(nums) == 0:
return 0.0
return sum(nums) / len(nums)
def grt_mean(nums):
m = mean(nums)
return [i for i in nums if i > m]
For example
>>> nums = [1,2,3,4,5,6,7,8,9]
>>> mean(nums)
5.0
>>> grt_mean(nums)
[6, 7, 8, 9]
Same idea for "above 90"
def above90(nums):
return [i for i in nums if i > 90]
>>> above90([88, 89, 90, 91, 92, 93])
[91, 92, 93]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.