# 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)
``````

``````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")

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')

``````
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
AH! thanks just taking out the el worked. just so used to following up the ifs with the elifs!
Commented:
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.
Author 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')
``````
Commented:
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
Author Commented:
Thank you both!!
Author Commented:
how do I give @gelonida an assit?
Commented:
You're welcome. never mind the assist points for this question. :-)

Does everything work now?
Author Commented:
yes, thank you!

I'm terrible at spotting typos!
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.