在if语句Python中使用附加变量

时间:2018-02-21 00:58:17

标签: python python-3.x if-statement syntax append

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

这一切都很好,正如你所看到的那样,我正在尝试使用我的测试类别(看似不必要的打印语句的原因)除了一件事:附加声明

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

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

这是错误消息:

  

属性错误:' str'对象没有属性'追加

这是我的代码:

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)

3 个答案:

答案 0 :(得分:1)

对于这种存储,您可以使用字典作为问候类别,如下所示:

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)

我从代码中删除了打印报表,但他们没有提供任何背景信息。 检查python文档中的字典。

答案 1 :(得分:0)

我认为他的意思是你应该创建一个带有花括号的字典,这些字母包含像你的类别一样的键。然后,您可以通过调用该特定键来附加到该字典。我认为你需要的是一本关于词典的教程。该平台用于更多地讨论错误。

答案 2 :(得分:0)

假设你想要将项目添加到greetingInput 通过input,如果greetingInput将其视为string个对象,而不是您要添加的list对象。

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

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