pythonnameerror

NameError in Python code saying it is not defined


This is the image of the error I am getting. I've tried many things to get this code to work, but no matter what, I keep getting a nameError when I try calling the function displayRent(). The code is to prompt a user on what apartment they want to rent, and it will display back their rent and deposit total. I am using Python 3.12 for this class, I asked my professor for help but she isn't responding of course.

#This is the first function that will display the menu
#for the apartment types so users know their options.
def menu():
    print("""

    Type of Apartments for rent

          1) Studio
          2) One Bedroom
          3) Two Bedroom
          4) EXIT PROGRAM

          """)
#This calls the functions so it will actually display.
menu()


#This function is going to get the users input
def getType():

    #This list is what the user will choose from
    choicelist=[1,2,3,4]
    choice = int(input("Enter the apartment type you wish to rent (1-3 or 4 \
to EXIT PROGRAM): "))

    #This validates whether they put in a valid
    #input or not.
    while choice not in choicelist:
        print("ERROR: Invalid apartment type...MUST be 1-4!")
        choice = int(input("Enter the apartment type you wish to rent (1-3 or 4 \
to EXIT PROGRAM): "))
    if choice == 4:  
        print("""EXITING THE PROGRAM...
Thanks for inquiring about our apartments for rent!""")

        
    furnished=input("Do you want the apartment furnished (Y/N)? ").upper()

    #This returns the values so we can use them
    #in other functions.
    return choice, furnished


#This function will determine their prices
#based on what input they gave.
def determineRent(choice, furnished):


    #These are lists that help with determining the
    #prices based on what the user selected. It keeps
    #it all in one place. 
    deposits=[0,800, 1000, 1200]
    unfurnished= [0,1200, 1500, 1800]
    Furnished_= [0,1500, 1800, 2050]

    deposit = deposits[choice]
    
    if furnished == 'Y':
        rent= Furnished_[choice]
    else:
        rent=unfurnished[choice]

    return deposit, rent


#This is where everything is displayed so
#the user knows how much they must pay for a
#deposit and for the rent as well. 
def displayRent(choice, furnished, deposit, rent):
    description = ["", "Studio", "One-Bedroom", "Two-Bedroom"]
    print("Discription:", description[choice],"Furnished" if furnished=="Y" else "Unfurnished")
    print("Deposit: ${}".format(deposit))
    print("Rent: ${}".format(rent))
    print()



getType()
displayRent(rent, deposit, choice, furnished)

I tried rewriting the parameters for displayRent() but whichever parameter was first, it would just give me the same nameError code. I looked over it a lot, and I just cannot find where the mistake could be.


Solution

  • You need to call the functions that determine the variables and assign the variables to the results.

    choice, furnished = getType()
    deposit, rent = determineRent(choice, furnished)
    displayRent(rent, deposit, choice, furnished)