NameError: name 'furnish' is not defined Python

Viewed 41
def getType():

    inp=int(input("\t<<<<<APARTMENT RENTAL PROGRAM>>>>>\n1 studio\n2 One-Bedroom\n3 Two-Bedroom\n4 Exit\nEnter Your Choice ::"))

    while(inp>4 or inp<0):

        print("Enter Valid Choice!!")

        inp=int(input("\t<<<<<APARTMENT RENTAL PROGRAM>>>>>\n1 studio\n2 One-Bedroom\n3 Two-Bedroom\n4 Exit\nEnter Your Choice ::"))

    if(inp==4):
      return 4,0

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

    valid=['y','n','Y','N']

    while(furnished not in valid):

        print("Enter Valid Choice!!")

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

    if(furnished == 'Y' or furnished=='y'):

        return inp,1

    return inp,0

def determineRent(kind,furnish):

    rents=[[400,600,750],[500,750,900],[600,925,1025]]

    return rents[kind-1][0],rents[kind-1][furnish+1]

def displayRent(kind,furnish,rent,deposit):

    if(kind==1):

        print("\tStudio")

    elif (kind == 2):

        print("\tOne-Bedroom")

    elif(kind==3):

        print("\tTwo-Bedroom")

**if (furnish == 1):
    print("\tFurnished")**

else:

    print("\tUnfurnished")

print("\tDeposit : $",deposit)

print("\tRent : $",rent)

I am trying to get rid of my syntax issues and I cannot get my statement to work (bolded) I included the entire code for reference. I tried redefining furnish and changing it to furnished to no avail.

3 Answers

Code inside function definitions (indented under a def statement) is not executed until you call those functions, so will not cause NameErrors when running the program. I'm not sure if this is your intention, but based on the way you have your code indented above, the only code that is being executed is the final if statement and 2 print statements. This should make it clearer:

def getType():

    *** definition of getType function ***

def determineRent(kind,furnish):

    *** definition of determineRent function ***

def displayRent(kind,furnish,rent,deposit):

    *** Definition of displayRent function ***


if (furnish == 1):
    print("\tFurnished")

else:

    print("\tUnfurnished")

print("\tDeposit : $",deposit)

print("\tRent : $",rent)

Since you never call the getType, determineRent, or displayRent functions, the interpreter just passes through storing those in memory without attempting to actually execute the code. So the first line it actually attempts to execute is:

if (furnish == 1):

But you haven't defined a variable called furnish at this point. That's why you get the NameError. If you write furnish = 1 above this line you'll see that it works (but next you get NameError for deposit instead).

while(furnished not in valid):
    print("Enter Valid Choice!!")

That looks like an infinite loop to me :)

Assuming that these are functions called from another module, and that the code erroring out was suposed to be inside the displayRent function, your problem is indentation.

Python has an indentation based syntax, which means that your spaces/tabs at the beginning of the line matter a lot. In this case you have none on your line if (furnish == 1): and on the next lines, which means that Python will understand that they should be considered at the global level of that module. If you want it to be within the displayRent function you need to indent correctly, i.e. add leading tab/spaces to match the rest of the function code. Like this:

def getType():
    inp=int(input("\t<<<<<APARTMENT RENTAL PROGRAM>>>>>\n1 studio\n2 One-Bedroom\n3 Two-Bedroom\n4 Exit\nEnter Your Choice ::"))
    while(inp>4 or inp<0):
        print("Enter Valid Choice!!")
        inp=int(input("\t<<<<<APARTMENT RENTAL PROGRAM>>>>>\n1 studio\n2 One-Bedroom\n3 Two-Bedroom\n4 Exit\nEnter Your Choice ::"))
    if(inp==4):
      return 4,0

    furnished=input("Do you want the apartment furnished(Y/N)?")
    valid=['y','n','Y','N']
    while(furnished not in valid):
        print("Enter Valid Choice!!")

    furnished = input("Do you want the apartment furnished(Y/N)?")
    if(furnished == 'Y' or furnished=='y'):
        return inp,1

    return inp,0

def determineRent(kind,furnish):
    rents=[[400,600,750],[500,750,900],[600,925,1025]]
    return rents[kind-1][0],rents[kind-1][furnish+1]

def displayRent(kind,furnish,rent,deposit):
    if(kind==1):
        print("\tStudio")
    elif (kind == 2):
        print("\tOne-Bedroom")
    elif(kind==3):
        print("\tTwo-Bedroom")

    if (furnish == 1):
        print("\tFurnished")
    else:
        print("\tUnfurnished")

    print("\tDeposit : $",deposit)
    print("\tRent : $",rent)

Documentation for reference and better understanding: Python Docs - Indentation

Related