Question: Would anyone be able to identify why i keep getting NameError: name 'db' is not defined when i pick choice == 3 or choice ==4

Would anyone be able to identify why i keep getting NameError: name 'db' is not defined when i pick choice == 3 or choice ==4 in my client module? I have pasted the two (client apartment_db) modules that are interacting with each other on this below:

client module below

from apartment import Apartment from apartment_db import Apartment_db from tenant import Tenant from tenant_database import Tenant_db

input_file = 'apartmentDB.txt' output_file = 'tenant'

def moduleConnector(aptFile): db = Apartment_db() tdb = Tenant_db() db.loadApartments(aptFile)

moduleConnector(input_file)

def read_file(): with open(input_file) as f: content = f.readlines() full_lines = [x.strip() for x in content] full_lines = [line.split(' ') for line in full_lines] for line in full_lines: line[0] = int(line[0]) line[1] = int(line[1]) line[2] = int(line[2]) line[3] = int(line[3]) return full_lines read_file()

def labMenu(): print('Enter 1 to Rent/Lease Apartment') print('Enter 2 to Search Available Apartments') print('Enter 3 to Make Apartment Available') print('Enter 4 to List Available apartments') print('Enter 5 to List Rented Apartments') print('Enter 6 to Display Tenant Information') print('Enter 7 to Add New Apartment') print('Enter 8 to Exit') done = False

while not done: labMenu() choice = int(input('Please make an entry: '))

if choice == 1:

def menu(): full_lines = read_file() apt_bdrm = int(input('Enter minimum number of bedrooms required: ')) apt_bath = int(input('Enter minimum number of baths required: ')) apt_rent = int(input('Enter maximum amount of rent: ')) print(' Available apartments for lease: ')

available_rooms = [] for index in range(len(full_lines)): line = full_lines[index]

if line[1] >= apt_bdrm and line[2] >= apt_bath and line[3] <= apt_rent and line[4] == 'A': available_rooms.append(line[0]) print('Available apartments are: ' + str(line[0]) + ' with bedrooms ' + str(line[1]) + ' baths ' + str(line[2]) + ' and cost'+ str(line[3]))

if len(available_rooms) > 0: choice = int(input('Enter number that corresponds to the apartment:(-1 for main menu): ')) if choice == -1: done = True else: if choice in available_rooms:

fName = input('Enter first name: ') lName = input('Enter last name: ')

with open(output_file, 'a') as myfile: myfile.write(fName + ' ' + lName + ' ' +str(choice)) for line in full_lines: if line[0] == choice: line[4] = 'R' out = '' for line in full_lines: line = ' '.join(str(x) for x in line) line = line + ' ' out = out + line with open(input_file, 'w') as f: f.write(out) else: print('Select Available apartment') menu() else: print('No apartment meets your search') menu()

menu()

#begin number2

elif choice == 2:

def menu(): full_lines = read_file() apt_bdrm = int(input('Enter minimum number of bedrooms required: ')) apt_bath = int(input('Enter minimum number of baths required: ')) apt_rent = int(input('Enter maximum amount of rent: ')) print('Available apartments for lease: ')

available_rooms = [] for index in range(len(full_lines)): line = full_lines[index]

available_rooms = [] for index in range(len(full_lines)): line = full_lines[index] if line[1] >= apt_bdrm and line[2] >= apt_bath and line[3] <= apt_rent and line[4] == 'A': available_rooms.append(line[0]) print('Available apartments are: ' + str(line[0]) + ' with bedrooms ' + str(line[1]) + ' baths ' + str(line[2]) + ' and cost '+ str(line[3]))

if len(available_rooms) > 0: choice = int(input('Enter -1 for main menu: ')) if choice == -1: done = True

else: print('No apartment meet your search') done = True menu() elif choice == 3:

def makeAvailable(db,tdb):

aptNum = str(input('Enter apartment number: '))

targetApt = db.getApartment(aptNum)

if targetApt == None: print('No apartments matched that number') return

if(targetApt.getApartmentStatus() == 'A'): print('Apartment number is already available') return else: targetApt.setApartmentStatus('A') tdb.removeTenant(aptNum) print('Tenant removed') makeAvailable(db, tdb) elif choice == 4: def listAvailApartments(db,tdb): results = db.getAvailApartments() if len(results) > 0: print(' Avaialable apartments: ') displaySearchResults(results) else: print('no available apartments')

listAvailApartments() elif choice == 5: #list rented apartments def listRentedApartments(db, tdb): results = db.getRentedApartments() if len(results) > 0: print(' Rented apartments: ') displaySearchResults(results) else: print('no rented apartments') elif choice == 6: #display tenant info tenant = input("Enter apartment number: ")

def displayTenant(tdb):

foundTenant = [] for tenantX in self.tenant_db: if tenantX.getaptNum() == aptNum: foundTenant = tenantX break return foundTenant

getTenant(apt_num)

elif choice ==7: newAptNum = int(input('Enter new apartment number: ')) newAptBedrms = int(input('Enter new number of bedrooms: ')) newAptBath = int(input('Enter new number of baths: ')) newAptRent = int(input('Enter new rent amount: '))

def addNewApartment(db): Apartment_db.append(apartment) addApartment(newAptNum) #enter 8th option ending program elif choice == 8: #display report print('Total Number of Apartments: ' + db.getTotalApartments()) print('Total Number of Apartments leased: ' + db.getTotalRented()) print('Total Number of Apartments available: ' + db.getTotalAvailable()) print('Total Number of Tenants: ' + tdb.getcountTenants()) done = True

apartment_db module below

import apartment

class Apartment(object): def __init__(self, num, bed, bath, rent, status): self.apt_num = num self.apt_bedrm = bed self.apt_bath = bath self.apt_rent = rent self.apt_status = status

def __repr__(self): return "(num:%s bed:%s bath:%s rent:%s status:%s) "%(self.apt_num,self.apt_bedrm,self.apt_bath,self.apt_rent,self.apt_status)

class Apartment_db(object): def __init__(self): self.apartments = []

def searchDB(self, apt_bedrm, apt_baths, apt_rent, apt_status): results = [] for apt in self.apartments: if ( (apt.getApt_bedrm() >= apt_bedrm) & (apt.getApt_baths() >= apt_baths) & (apt.getApt_rent() <= apt_rent) & (apt.getApt_status().strip() == apt_status) ): results.append(apt) return results

def addApartment(self,apartment): self.Apartment_db.append(apartment) print('Apartment added successfully')

def getApartment(self, apt_num): for apartment in self.apartments: if(apartment.apt_num == apt_num): return apartment return none

def getAvailApartments(self): availApartments = [] for apartment in self.apartments: if(apartment.apt_status == 'A'): availApartments.append(apartment) return availApartments

def getRentedApartments(self): rentedApartments = [] for apartment in self.apartments: if(apartment.apt_status == 'R'): rentedApartments.append(apartment) return rentedApartments

def changeApartmentStatus(self,apartmentNumber, status): found = False for apartment in self.apartments: if(apartment.apt_num == apartmentNumber): found = true if (apartment.apt_status == status): print('apartment status not changed') else: apartment.apt_status = status if(found == False): print('apartment number is not valid')

def getTotalApartments(self): return len(self.apartment)

def getTotalAvailable(self): return len(getAvailApartments(self))

def getTotalRented(self): return len(getRentedApartments(self))

def loadApartments(self, aptFile): file = open(aptFile, "r") for line in file.readlines(): line = line.split(" ") a = Apartment(int(line[0]), int(line[1]), int(line[2]), int(line[3]),(line[4])) self.apartments.append (a)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!