0

# Python Data Structures Fancy Houses Task Question

Hey Guys I have a Problem. I think my code solves the problem, but i can not pass because sololearn shows that my answer is not correct and I can not look what is wrong because the case is closed. Maybe some of you guys could pass the task and find the problem. Task: You are analyzing house prices. The given code declares a list with house prices in the neighborhood. You need to calculate and output the number of houses that have a price that is above the average. To calculate the average price of the houses, you need to divide the sum of all prices by the number of houses. My solution: prices = [125000, 78000, 110000, 65000, 300000, 250000, 210000, 150000, 165000, 140000, 125000, 85000, 90000, 128000, 230000, 225000, 100000, 300000] #your code goes here average_price = int(sum(prices))/int(len(prices)) # print(int(average_price)) for n in prices: if n < int(average_price): prices.remove(n) print(len(prices)) Average Price: 159777 Output (Number of houses above the average price): 11 Sorry for the long post

22nd May 2021, 12:20 PM
Eduard Rein
9 Respostas
+ 3
oh...You are changing original list by prices.remove(N) so there it skipping next iteration value for n in for n in prices: Alternatively use new list to modify or to add. Else take copy in loop as for n in prices.copy():
22nd May 2021, 12:38 PM
Jayakrishna 🇮🇳
+ 2
Looking at your code, there is a problem. There are only 7 values above average. You already got the average and you have the list of numbers: num_abv_avg = len([p for p in prices if p > average_price])
22nd May 2021, 12:40 PM
Slick
+ 1
I think, May there need <= instead of <.
22nd May 2021, 12:26 PM
Jayakrishna 🇮🇳
+ 1
prices = [125000, 78000, 110000, 65000, 300000, 250000, 210000, 150000, 165000, 140000, 125000, 85000, 90000, 128000, 230000, 225000, 100000, 300000] #your code goes here average_price = sum(prices)/len(prices) # print(int(average_price)) for n in prices[:]: #MODIFIED PART if n < int(average_price): prices.remove(n) print(prices,len(prices)) Do this instead.. It'll work for sure.. Should be very careful while using the remove()
22nd May 2021, 12:42 PM
+ 1
Ohh yeah thanks
22nd May 2021, 12:42 PM
Eduard Rein
+ 1
This one worked out. Thanks @slick prices = [125000, 78000, 110000, 65000, 300000, 250000, 210000, 150000, 165000, 140000, 125000, 85000, 90000, 128000, 230000, 225000, 100000, 300000] #your code goes here average_price = sum(prices)/len(prices) num_abv_avg = len([p for p in prices if p > average_price]) print(num_abv_avg)
22nd May 2021, 4:02 PM
Eduard Rein
0
Thanks for your anser but this should not change the output of the code
22nd May 2021, 12:35 PM
Eduard Rein
0
I will try thanks))
22nd May 2021, 12:41 PM
Eduard Rein
- 1
11 is not right, look at the prices there are only 7 with higher price than 159777. Take new variables to avoid to change the original list: num_high_price = 0 for i in prices: if i >= average: num_high_price += 1 print(num_high_price)
21st Jun 2021, 10:59 AM
Angela