Python:属性不能附加

2024-09-30 14:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解决这个问题,当我使用(def checkIn)函数时,它显示'str'对象没有'append'属性。我已经为petName=[]创建了一个列表,但它似乎无法追加输入。你知道吗

staffID = 'admin'
password = 'admin'
petType = ['Dog', 'Cat', 'Bird', 'Rodent']
petName = []

def loginFunction(s, p):
    # Login inputs
    staffID = input("Enter Staff ID: ")
    password = input("Password: ")

    loginTrust = False
    while (loginTrust is False):
        if (staffID == 'admin') and (password == 'admin'):
            print("Successfully logged in")
            loginTrust = True

        else:
            print("Wrong ID or Password. Please enter again. ")
            loginTrust = False
            staffID = input("Enter Staff ID: ")
            password = input("Password: ")

发行

def checkIn(petType, petName):
    petName = str(input("Enter pet name: "))
    petName.append(petName)
    tempList.append(petName)
    boardedPets.extend(tempList)

def FrontDeskMenu():
    print("\nTaylor's Pet Hotel\nFront Desk Admin")
    print("A. Check in pets")
    print("B. Check out pets")
    print("C. Payment")
    print("D. Rooms Availability")
    print("E. History")
    print("F. Exit\n")

    userInput = input("What would you like to do today?: ")

    inputCheck = False
    while (inputCheck is False):
        if (userInput.lower() == 'a'):
            checkIn(petType, petName)
            inputCheck = True
        elif (userInput.lower() == 'f'):
            quit()
        else: 
            print("Invalid value! Please try again.")
            userInput = input("What would you like to do today?: ")
            inputCheck = False


loginFunction(staffID, password)
FrontDeskMenu()

Tags: falseinputadmindefpasswordprintenterappend
2条回答

您已经声明了一个字符串并尝试使用string“append()”,它是一个方法,可以与list[]一起使用。对字符串使用不同的名称,如pet\u name、pet\u name1、petName。你也应该申报你的名单-圣殿骑士和寄宿宠物。你知道吗

tempList = []
boardedPets = []

那就这么做 def签入(petType,petName):

 # pet_name= str(input("Enter pet name: ")) - no need to convert into string , input method gives always string so do not convert it
pet_name = input("Enter pet name: ")
petName.append(pet_name)
tempList.append(petName)
boardedPets.extend(tempList)

首先,您没有初始化petName=[]templast=[]boardedPets=[]并且您正在覆盖您的petName。你知道吗

代码看起来像,我希望它能正常工作

staffID = 'admin'
password = 'admin'
petType = ['Dog', 'Cat', 'Bird', 'Rodent']
petName = []
tempList = []
boardedPets = []

def loginFunction(s, p):
    # Login inputs
    staffID = input("Enter Staff ID: ")
    password = input("Password: ")

    loginTrust = False
    while (loginTrust is False):
        if (staffID == 'admin') and (password == 'admin'):
            print("Successfully logged in")
            loginTrust = True

        else:
            print("Wrong ID or Password. Please enter again. ")
            loginTrust = False
            staffID = input("Enter Staff ID: ")
            password = input("Password: ")

def checkIn(petType, petName):
    petName1 = str(input("Enter pet name: "))
    petName.append(petName1)
    tempList.append(petName1)
    boardedPets.extend(tempList)

def FrontDeskMenu():
    print("\nTaylor's Pet Hotel\nFront Desk Admin")
    print("A. Check in pets")
    print("B. Check out pets")
    print("C. Payment")
    print("D. Rooms Availability")
    print("E. History")
    print("F. Exit\n")

    userInput = input("What would you like to do today?: ")

    inputCheck = False
    while (inputCheck is False):
        if (userInput.lower() == 'a'):
            checkIn(petType, petName)
            inputCheck = True
        elif (userInput.lower() == 'f'):
            quit()
        else: 
            print("Invalid value! Please try again.")
            userInput = input("What would you like to do today?: ")
            inputCheck = False


loginFunction(staffID, password)
FrontDeskMenu()

Code Output

相关问题 更多 >