Getting all instances that meet the criteria to print, not just the first

Hi! The purpose of the program is to get the user to enter in there criteria for an apartment, and have all of the apartments that meet that criteria print out. However, my code only works to find the first apartment that meets the criteria and doesn't continue moving through the list. Any ideas on how to make this work while keeping my apartments class objects?

class Apartments(object):
    def __init__(self,aptNum, rent, rooms, bath):
            self.aptNum = aptNum
            self.rent = rent
            self.rooms = rooms
            self.bath = bath
    
    def getApt(self):
        return '{:<10} {:<10} {:<10} {:<10}'.format(self.aptNum,self.rent,self.rooms,self.bath)

Open in new window


import Apartments

apt_1= Apartments.Apartments(111,1000,1,1)
apt_2= Apartments.Apartments(112,2000,2,1)
apt_3= Apartments.Apartments(113,2500,2,2)
apt_4= Apartments.Apartments(114,3000,3,2)
apt_5= Apartments.Apartments(115,3500,3,3)

directory=[apt_1,apt_2,apt_3,apt_4,apt_5]


while True:
    rentInput=input('Enter maximum amount of rent: ')
    roomInput=input('Enter minimum number of bedrooms: ')
    bathInput=input('Enter minimum number of baths: ')
    try:
        val = int(rentInput)
        val2 = int(roomInput)
        val3 = int(bathInput)
        if val < 0:  
            print("Sorry, input must be a positive integer, try again")
            continue
        if val2 < 0:  
            print("Sorry, input must be a positive integer, try again")
            continue
        if val3 < 0:  
            print("Sorry, input must be a positive integer, try again")
            continue
        break
    except ValueError:
        print("Sorry, input must be a positive integer, try again")
        
header='{:10} {:10} {:10} {:10}'.format('Aparment#','Rent','Rooms','Bathrooms')
    if int(rentInput)>= apt_1.rent and int(roomInput)<= apt_1.rooms and int(bathInput)<= apt_1.bath:
        print(apt_1.getApt())
        continue
    elif int(rentInput)>= apt_2.rent and int(roomInput)<= apt_2.rooms and int(bathInput)<= apt_2.bath:
        print(apt_2.getApt())
        continue
    elif int(rentInput)>= apt_3.rent and int(roomInput)<= apt_2.rooms and int(bathInput)<= apt_2.bath:
        print(apt_3.getApt())
        continue
    elif int(rentInput)>= apt_4.rent and int(roomInput)<= apt_2.rooms and int(bathInput)<= apt_2.bath:
        print(apt_4.getApt())
        continue
    elif int(rentInput)>= apt_5.rent and int(roomInput)<= apt_2.rooms and int(bathInput)<= apt_2.bath:
        print(apt_5.getApt())
    
    else:
        print('Sorry, no rooms available that meet that criteria')
    

Open in new window

L WardAsked:
Who is Participating?
 
Rgonzo1971Commented:
Hi,

Why use elif when the ifs are not mutually exclusive in your logic?
You could test first if all of them if one  is valid else "print('Sorry, no rooms available that meet that criteria')"
and then test each one of them individually with a series of if (with no elif)

Regards
0
 
L WardAuthor Commented:
AH! thanks just taking out the el worked. just so used to following up the ifs with the elifs!
0
 
gelonidaCommented:
Yep Rgonzo1971 identified the problem in your code.

As you stored all apartments in the variable directory you could write a for loop that iterates through directory  and seraches for all  matches.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
L WardAuthor Commented:
So this works, sometimes, but for some reason not everytime. Example, if I run the code with inputs 3500,  2, 2, even though it should meet two sets of criteria it prints the else scenario. However, if I put in 3500, 2, 1 - it pulls up all the right results. Does anyone see how that's happening?
if int(rentInput)>= apt_1.rent and int(roomInput)<= apt_1.rooms and int(bathInput)<= apt_1.bath:
    print(apt_1.getApt())
if int(rentInput)>= apt_2.rent and int(roomInput)<= apt_2.rooms and int(bathInput)<= apt_2.bath:
    print(apt_2.getApt())
if int(rentInput)>= apt_3.rent and int(roomInput)<= apt_2.rooms and int(bathInput)<= apt_2.bath:
    print(apt_3.getApt())
if int(rentInput)>= apt_4.rent and int(roomInput)<= apt_2.rooms and int(bathInput)<= apt_2.bath:
    print(apt_4.getApt())
if int(rentInput)>= apt_5.rent and int(roomInput)<= apt_2.rooms and int(bathInput)<= apt_2.bath:
    print(apt_5.getApt())    
else:
    print('Sorry, no rooms available that meet that criteria')

Open in new window

0
 
gelonidaCommented:
Your code has also some typos.

line 40 partially compares with apt_3 and partially with apt_2
line 43 partially compares with apt_4 and partially with apt_2
line 46 partially compares with apt_5 and partially with apt_2

That's the danger of copy paste, which probably every developer encountered already
0
 
L WardAuthor Commented:
Thank you both!!
0
 
L WardAuthor Commented:
how do I give @gelonida an assit?
0
 
gelonidaCommented:
You're welcome. never mind the assist points for this question. :-)

Does everything work now?
0
 
L WardAuthor Commented:
yes, thank you!

I'm terrible at spotting typos!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.