简体   繁体   English

NameError:未定义名称“提供” Python

[英]NameError: name 'furnish' is not defined Python

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.我尝试重新定义家具并将其更改为家具无济于事。

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

That looks like an infinite loop to me:)对我来说,这看起来像是一个无限循环:)

Code inside function definitions (indented under a def statement) is not executed until you call those functions, so will not cause NameError s when running the program.在调用这些函数之前,不会执行 function 定义(在def语句下缩进)中的代码,因此在运行程序时不会导致NameError 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.我不确定这是否是您的意图,但根据您在上面缩进代码的方式,唯一正在执行的代码是最后的if语句和 2 个打印语句。 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.由于您从不调用getTypedetermineRentdisplayRent函数,解释器只是将这些函数存储在 memory 中,而不尝试实际执行代码。 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.但是此时您还没有定义一个名为furnish的变量。 That's why you get the NameError .这就是你得到NameError的原因。 If you write furnish = 1 above this line you'll see that it works (but next you get NameError for deposit instead).如果你在这一行上方写上furnish = 1 ,你会看到它可以工作(但接下来你会得到NameError来代替deposit )。

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.假设这些是从另一个模块调用的函数,并且错误的代码应该在displayRent function 内,那么您的问题是缩进。

Python has an indentation based syntax , which means that your spaces/tabs at the beginning of the line matter a lot. Python 具有基于indentation based syntax ,这意味着行首的空格/制表符很重要。 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 (furnish == 1):和下一行中没有任何内容,这意味着 Python 将理解应该在该模块的全局级别上考虑它们。 If you want it to be within the displayRent function you need to indent correctly, ie add leading tab/spaces to match the rest of the function code.如果您希望它在displayRent function 内,您需要正确缩进,即添加前导制表符/空格以匹配 ZC1C425268E68385D14AB5074C17A9 代码的 rest。 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供参考和更好理解的文档: Python Docs - Indentation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM