So, I'm taking some classes and learning how to write sample programs in Python, and I've stalled out pretty hard on one of the sample programs. The current exercise wants me to do the following:

  1. Create an empty list home_prices
  2. While loop asks for a home price
  3. In the while loop, append a home price to the home_prices loop
  4. Print the entire home_prices list, one price per line, with a title that prints just once before the list of prices.
  5. Ask user for a mid-level price (not too high, not too low)
  6. Print a list of all home prices less than or equal to that mid-level price
  7. Print a list of all home prices above the mid-level price.
  8. Get the total of all the home prices; display that total,
  9. Also display the least expensive and most expensive home in the list

Steps one 1-4 are easy, and I've completed them. Steps 5-7 is where I run into a brick wall. I don't know how to split lists based on a numeric comparison to individual items within a list, since you can't compare a list and an integer directly. I was chased out of Stack Overflow for asking a stupid question, so I'm sure it's simple and I'm just blanking on something, but I could really use some help.

My stab-in-the-dark code that doesn't work is below:

import utils

home_prices = []
answer = 'yes'
while answer == 'yes':
    home_prices.append(int(input("Please enter a house price between 75000 and 500000: ")))
    answer = utils.yes_or_no("Do you have another house price? (Must have at least twelve for program to work    accurately.)")
#end while

print('Home prices:')
n = len(home_prices)
i = 0
while i < n:
    name = home_prices[i]
    i = i + 1
#end while

home1 = []
home2 = []
input = int(input("Please enter a medium priced house: "))

if home_prices => input:

n = len(home1)
i = 0
while i < n:
    name = home1[i]
    i = i + 1

n = len(home2)
i = 0
while i < n:
    name = home2[i]
    i = i + 1

Edited 2 Years Ago by keegansch: Correcting typo

You are changing input function to mean input values hiding the original meaning. Your approach should work even if it is not ideal. Using index position loops is also ugly and it is better to iterate over items in for loop. Things get much clearer once you learn about functions and list comprehensions.

Appreciate the response.

Yeah, assigning a value to "input" is sloppy... I changed it to something less confusing.

Anyway, this is the error I get when I try to run the above code:

Traceback (most recent call last):
  File "C:/Users/Keegan/Dropbox/CIS Homework/Lab 4/Lab 4.py", line 27, in <module>
    while home_prices <= int(250000):
TypeError: unorderable types: list() <= int()

Using my current (limited) understanding of how lists work, how do I get around that?

You should be heading towards something like this later (ignoring price limits etc):

# for Python2 compatibility
from __future__ import print_function, division
    input = raw_input

houses = []
while True:
    value = input('House %i: ' % (len(houses) + 1))
    if value:
limit = int(input('Give limit: '))
print('Less or limit')
print('\n'.join(str(house) for house in houses if house <= limit))
print('\n'.join(str(house) for house in houses if house > limit))
print('Total price of houses', sum(houses))
print('Average house price', sum(houses) /len(houses))

""" Example session:
House 1: 3123
House 2: 342344
House 3: 435345
House 4: 34534
House 5: 5646
House 6: 5645645
House 7: 21342342
House 8: 123213
House 9: 21341
House 10: 
Give limit: 40000
Less or limit
Total price of houses 27953533
Average house price 3105948.11111

Edited 2 Years Ago by pyTony

And then ... perhaps next step ... 'revise code to crash proof input' ... i.e. to accept only valid user input:

# takeInHousePrices.py #

def takeInInt( msg, low, high ):
    while 1:
            val = int( input( msg ) )
            if val == 0 or low*1000 <= val <= high*1000:
                return val
                print( 'Prices valid here are those in range {}k..{}k'.\
                       format( low, high ))
        except( ValueError ):
            print( '\nValue Error!  Only integers are valid here!' )

houses = []
while True:
    value = takeInInt( 'Enter price for house #{} (0 to exit): '.format(len(houses)+1), 50, 100 )
    if value:

if houses:    
    mid = takeInInt( "Enter a 'middle' price: ", 50, 100 )

    print( 'Houses with a price less than or equal to the mid price of {}:'.format(mid) )
    tmpLst = [str(house) for house in houses if house <= mid]
    print( '\n'.join(tmpLst) )

    print( 'Houses with a price greater than the mid price of {}:'.format(mid) )
    tmpLst = [str(house) for house in houses if house > mid]
    print( '\n'.join(tmpLst) )

    ssum = sum(houses)
    print( 'Total price of {} houses sums to {}'.format(len(houses), ssum) )
    print( 'Average house price for these {} houses was {:.2f}'.format( len(houses), ssum/len(houses)) )

Edited 2 Years Ago by David W

This article has been dead for over six months. Start a new discussion instead.