Python 在if语句中使用带变量的append

Python 在if语句中使用带变量的append,python,python-3.x,if-statement,syntax,append,Python,Python 3.x,If Statement,Syntax,Append,我正在尝试创建一种方法,通过一个用户界面将输入值添加到我的列表中,该界面会询问您要添加的类别和单词 这一切都很好,正如您所看到的,我在我的测试类别中进行了尝试(似乎不需要打印语句的原因)除了一件事:append语句 我想找到一种方法,将输入的类别放在附加之前的位置,这样我想添加的短语就会添加到该类别中 我不知道该放什么,所以我尝试了很多东西:list、(list)、userCategory(现在在那里)和(userCategory)。我认为这可能是一个语法错误,但它只是告诉我,这些名称不能附加到

我正在尝试创建一种方法,通过一个用户界面将输入值添加到我的列表中,该界面会询问您要添加的类别和单词

这一切都很好,正如您所看到的,我在我的测试类别中进行了尝试(似乎不需要打印语句的原因)除了一件事:append语句

我想找到一种方法,将输入的类别放在附加之前的位置,这样我想添加的短语就会添加到该类别中

我不知道该放什么,所以我尝试了很多东西:list、(list)、userCategory(现在在那里)和(userCategory)。我认为这可能是一个语法错误,但它只是告诉我,这些名称不能附加到

这是错误消息:

AttributeError:'str'对象没有属性'append'

这是我的代码:

testcategory = ['chicken',]
greetingInput = ['hi','HI','Hi','Hello','wazzup','hello','sup','Sup','howdy','Howdy','hey','Hey','What\'s hanging my dude?',]
greetingOutput = ['Hello, how are you?','How\'s it going?','Wazzup','What\'s hanging my dude?',]
greetingResponseP = ['good','great','ok','fine','okay','amazing','splendid','Good','Great','Ok','Fine','Okay','OK','Amazing','Splendid','allright','Allright',]
greetingResponseB = ['bad','sucky','lame','not good','horrible','cruddy','bloody horrible','terrible','Bad','Sucky','Lame','Not good','Horrible','Cruddy','Bloody horrible','Not Good','Bloody Horrible','Terrible']
statusInputandResponseP = ['Good, how are you?','I\'m great, how are you?','i\'m good, how are you?','Good how are you?','I\'m great how are you?','i\'m good how are you?','Im great, how are you?','im good, how are you','Good, how are you','I\'m great, how are you','i\'m good, how are you','Im great, how are you','im good, how are you','Good, hbu?','I\'m great, hbu?','i\'m good, hbu?','Good hbu?','I\'m great hbu?','i\'m good hbu?','Im great, hbu?','im good, hbu','Good, hbu','I\'m great, hbu','i\'m good, hbu','Im great, hbu','im good, hbu','Good how are you?','I\'m great how are you?','i\'m good how are you?','Im great how are you?','im good how are you','Good how are you','I\'m great how are you','i\'m good how are you','Im great how are you','im good how are you','Good hbu?','I\'m great hbu?','i\'m good hbu?','Im great hbu?','im good hbu','Good hbu','I\'m great hbu','i\'m good hbu','Im great hbu','im good hbu',]
statusInput = ['how are you','How are you','how about you','How about you','hbu','HBU','how are you?','How are you?','how about you?','How about you?','hbu?','HBU?','How\'s it going?','how\'s it going?','how\'s it going','How\'s it going','Hows it going?','Hows it going','How\'s it goin\'?','how\'s it goin\'?','how\'s it goin\'','How\'s it goin\'','Hows it goin\'?','Hows it goin\'','How\'s it goin?','how\'s it goin?','how\'s it goin','How\'s it goin','Hows it goin?','Hows it goin',]
userCategory = input("Enter Category: ")
def addInput(list):
        print (testcategory)
        userWord = input("Input the word: ")
        if userCategory == str(list):
                userCategory.append(userWord)
                print (testcategory)

addInput(userCategory)

我认为他的意思是,你们应该创建一个带有花括号的字典,上面有和你们的类别类似的键。然后,您可以通过调用某个键来附加到该字典。我想你需要的是一本字典教程。此平台用于讨论更多有关错误的信息。

假设您想将项目添加到
问候输入中
通过
input
,如果
greetingInput
,它将被视为
string
对象,而不是要添加的
list
对象

为了避免这种情况,您可以通过编号对列表进行分类。基于此,您可以使用
if..elsif
添加列表。功能代码可以如下所示进行修改

<code>
userWord = input("Input the word: ")
if userCategory == 1:
    greetingInput.append(userWord)
</code>

对于这种类型的存储,您可以使用字典来存储问候语类别,如下所示:

testcategory = 'chicken'
greeting = {}
greeting['greetingInput'] = ['hi','HI','Hi','Hello','wazzup','hello','sup','Sup','howdy','Howdy','hey','Hey','What\'s hanging my dude?',]
greeting['greetingOutput'] = ['Hello, how are you?','How\'s it going?','Wazzup','What\'s hanging my dude?',]
greeting['greetingResponseP'] = ['good','great','ok','fine','okay','amazing','splendid','Good','Great','Ok','Fine','Okay','OK','Amazing','Splendid','allright','Allright',]
greeting['greetingResponseB'] = ['bad','sucky','lame','not good','horrible','cruddy','bloody horrible','terrible','Bad','Sucky','Lame','Not good','Horrible','Cruddy','Bloody horrible','Not Good','Bloody Horrible','Terrible']
greeting['statusInputandResponseP'] = ['Good, how are you?','I\'m great, how are you?','i\'m good, how are you?','Good how are you?','I\'m great how are you?','i\'m good how are you?','Im great, how are you?','im good, how are you','Good, how are you','I\'m great, how are you','i\'m good, how are you','Im great, how are you','im good, how are you','Good, hbu?','I\'m great, hbu?','i\'m good, hbu?','Good hbu?','I\'m great hbu?','i\'m good hbu?','Im great, hbu?','im good, hbu','Good, hbu','I\'m great, hbu','i\'m good, hbu','Im great, hbu','im good, hbu','Good how are you?','I\'m great how are you?','i\'m good how are you?','Im great how are you?','im good how are you','Good how are you','I\'m great how are you','i\'m good how are you','Im great how are you','im good how are you','Good hbu?','I\'m great hbu?','i\'m good hbu?','Im great hbu?','im good hbu','Good hbu','I\'m great hbu','i\'m good hbu','Im great hbu','im good hbu',]
greeting['statusInput'] = ['how are you','How are you','how about you','How about you','hbu','HBU','how are you?','How are you?','how about you?','How about you?','hbu?','HBU?','How\'s it going?','how\'s it going?','how\'s it going','How\'s it going','Hows it going?','Hows it going','How\'s it goin\'?','how\'s it goin\'?','how\'s it goin\'','How\'s it goin\'','Hows it goin\'?','Hows it goin\'','How\'s it goin?','how\'s it goin?','how\'s it goin','How\'s it goin','Hows it goin?','Hows it goin',]

def addInput(category='', word=''):
    print (testcategory)
    if category in greeting:
        greeting[category].append(word)
        print(greeting[category])
        print (testcategory)

userCategory = input("Enter Category: ")
userWord = input("Input the word: ")
addInput(userCategory, userWord)
我留下了代码中的
print
语句,尽管它们没有提供任何上下文信息。
有关词典的详细信息,请查看。

userCategory
是一个字符串。。。不能附加到字符串。我想您需要一个
dict
,其中的键是字符串,如
'greetingInput'
,它们具有相应的
列表
对象作为值。然后使用
input
中的
str
对象来恢复正确的
列表
,然后可以附加到itjuanpa.arrivillaga我不确定你的评论是什么意思。你能给我举个代码示例吗?