【发布时间】:2018-07-31 11:40:36
【问题描述】:
我正在尝试创建一种方法,通过一个用户界面将输入值添加到我的列表中,该用户界面询问您要添加的类别和单词。
一切正常,正如您所见,我正在使用我的测试类别进行尝试(看似不必要的打印语句的原因)除了一件事:附加语句。
我想找到一种方法将输入的类别放在追加之前的位置,以便将我要添加的短语添加到该类别中。
我不知道该放什么,所以我尝试了很多东西: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)
【问题讨论】:
-
userCategory是一个字符串......你不能附加到一个字符串。我想你想要一个dict,其键是'greetingInput'之类的字符串,具有相应的list对象作为值。然后使用input中的str对象来恢复正确的list,然后可以追加到它 -
juanpa.arrivillaga 我不确定您的评论是什么意思,您能给我看一个代码示例吗?
标签: python python-3.x if-statement syntax append