【问题标题】:Using append with a Variable in an if Statement Python在 if 语句 Python 中将 append 与变量一起使用
【发布时间】: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


【解决方案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)

我从您的代码中留下了 print 语句,尽管它们没有提供任何上下文信息。 查看Python Docs 了解有关词典的更多信息。

【讨论】:

    【解决方案2】:

    我认为他的意思是您应该创建一个带有大括号的字典,其中包含与您的类别类似的键。然后,您可以通过调用该特定键来附加到该字典。我认为您需要的是字典教程。这个平台用于更多地谈论错误。

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2018-07-30
        • 2015-01-15
        • 2012-07-01
        • 2020-11-06
        • 2019-03-16
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        • 2015-06-16
        相关资源
        最近更新 更多