-2
def top_quartile_prices(prices):
prices.sort()
n = int( len(prices ) / 4 )
top_list = []
    for i in range(len(prices) - 1, len(prices) - n - 1, -1):
    top_list.append(prices[i])
    top_list.sort()

prices = ['16.13', '32.2', '11.65', '39', '13.83', '25.33', '4.99', '13.1', '150', '37.81', '4.81', '4', '32.08',
          '12.66', '19.54', '48.52', '65.92', '18.2', '13.09', '19.32', '7.63', '2.23', '116.12', '3.66', '73.45',
          '54.71', '80.07', '15.99', '30.88', '10.91', '87.7', '6.34', '5.36', '20.66', '62.05', '88.98', '4.3',
          '63.42', '3.89', '34.01', '28.42', '4.69', '15.3', '55.22', '43.48', '11.73', '167.05', '11.17', '18.84',
          '44.31', '19.38', '29.38', '21.84', '57.59', '41.42', '23.91', '145.28', '14.76', '75.5', '2.32',
          '112.19', '38.87', '55.61', '13.35', '27.4', '6.49', '40.94', '8.66', '6.59', '45.73', '34.53', '8.47',
          '71.03', '108.39', '37.06']
top_quartile_prices(prices)
print('Top top quartile prices are :', top_quartile_prices(prices))

my function won't print the top quartile

blhsing
  • 91,368
  • 6
  • 71
  • 106
j.doe123
  • 1
  • 1
  • 2
    you never returned anything. just add `return toplist` in function `top_quartile_prices` – mad_ Oct 09 '18 at 15:37
  • 1
    Could you please fix the indentation of that code? We can't see what's inside the function definition, and what's outside it. True, we can _guess_, but it's better if we don't have to play guessing games. ;) – PM 2Ring Oct 09 '18 at 15:41
  • You need add a return in your function `top_quartile_prices` – Jose Sanchez Robles Oct 09 '18 at 15:45

4 Answers4

0

In the top_quartile_prices function, you are not returning the value that you are calculating so

print('Top top quartile prices are :', top_quartile_prices(prices))

just prints None.

Austin
  • 25,759
  • 4
  • 25
  • 48
Vikhyat Agarwal
  • 1,723
  • 1
  • 11
  • 29
0
def top_quartile_prices(prices):
    prices.sort()
    n = int( len(prices ) / 4 )
    top_list = []
    for i in range(len(prices) - 1, len(prices) - n - 1, -1):
        top_list.append(prices[i])
        top_list.sort()
    return top_list

prices = ['16.13', '32.2', '11.65', '39', '13.83', '25.33', '4.99', '13.1', '150', '37.81', '4.81', '4', '32.08',
          '12.66', '19.54', '48.52', '65.92', '18.2', '13.09', '19.32', '7.63', '2.23', '116.12', '3.66', '73.45',
          '54.71', '80.07', '15.99', '30.88', '10.91', '87.7', '6.34', '5.36', '20.66', '62.05', '88.98', '4.3',
          '63.42', '3.89', '34.01', '28.42', '4.69', '15.3', '55.22', '43.48', '11.73', '167.05', '11.17', '18.84',
          '44.31', '19.38', '29.38', '21.84', '57.59', '41.42', '23.91', '145.28', '14.76', '75.5', '2.32',
          '112.19', '38.87', '55.61', '13.35', '27.4', '6.49', '40.94', '8.66', '6.59', '45.73', '34.53', '8.47',
          '71.03', '108.39', '37.06']

top_quartile_prices(prices)

print('Top top quartile prices are :', top_quartile_prices(prices))
Lukas
  • 446
  • 3
  • 11
0

The indentation on this was off, assuming it was formatting when you pasted over, but your function has no return, include return top_list. Also the function call before your print statement is unnecessary.

def top_quartile_prices(prices):
    prices.sort()
    n = int( len(prices ) / 4 )
    top_list = []
    for i in range(len(prices) - 1, len(prices) - n - 1, -1):
        top_list.append(prices[i])
        top_list.sort()
    return top_list
# Top top quartile prices are : ['55.22', '55.61', '57.59', '6.34', '6.49', '6.59', '62.05', '63.42', '65.92', '7.63', '71.03', '73.45', '75.5', '8.47', '8.66', '80.07', '87.7', '88.98']
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
0

You have a couple of problems here:

1) As already pointed out you are not returning top_list from your function.

2) Your prices are strings so probably won't sort the way you expect them to. Add this at the top of your function:

prices = [float(p) for p in prices]

3) You don't need to iterate through the list to grab the top quartile. You can use a list slice, like this:

n = int( len(prices ) / 4 )
top_list = prices[len(prices)-n:]
T Burgis
  • 1,395
  • 7
  • 9