Python 代码中的 NameError 表示未定义

问题描述 投票:0回答:1

我尝试了很多方法来让这段代码正常工作,但无论如何,当我尝试调用函数 displayRent() 时,我总是收到 nameError 错误。该代码用于提示用户他们想要租哪套公寓,并且它将显示他们的租金和押金总额。我在这门课上使用 Python 3.12,我向我的教授寻求帮助,但她当然没有回应。

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

我尝试重写 displayRent() 的参数,但无论哪个参数是第一个,它只会给我相同的名称错误代码。我查了很多遍,就是找不到错误在哪里。

python nameerror
1个回答
0
投票

您需要调用确定变量的函数并将变量分配给结果。

choice, furnished = getType()
deposit, rent = determineRent(choice, furnished)
displayRent(rent, deposit, choice, furnished)
© www.soinside.com 2019 - 2024. All rights reserved.