【问题标题】:Trouble when wanting to delete a pairing from a list想从列表中删除配对时出现问题
【发布时间】:2014-09-21 19:56:00
【问题描述】:

我的一段代码允许用户删除已经在线索列表中的配对。但是,当我尝试运行此代码时,出现错误,我不确定如何解决这个问题...... 我删除配对的代码如下所示...

def delete_pairing(clues):
    found = True
    #USER INPUTS A LETTER AND SYMBOL
    letter=input("What letter would you like to delete? ").upper
    symbol=input("\nWhat symbol would you like to delete? ")
    #THE LETTER AND SYMBOL THE USER INPUTS BECOMES ONE STRING
    delClue = letter + symbol
    #IF THE delClue exists in clues, it will delete the pairing
    if delClue in clues:
    #CODE FOR REMOVING THE CLUE
        clues.remove(delClue)
    # LETS THE USER KNOW WHAT CLUES HAS BEEN DELETED
        print("\nClue ",(delClue)," has been deleted")
        print("\nYour clues are now...")
        print (clues)
    #If delClue doesn't exist in clues, it will print an error message    
    else:
        print("That clue does not exist  ")
    return clues

结果应该是,如果用户输入的字母和符号配对在线索列表中,则应该将其删除。否则,应该会出现一条错误消息,提示用户输入的字母和符号配对,在线索列表中不存在....

我遇到的错误...

    delClue = letter + symbol
TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'str'

【问题讨论】:

  • upper是一个方法,在它后面加()调用它。
  • Kasra - 我已将其添加到问题的末尾...

标签: python list typeerror


【解决方案1】:

正如the comment of Ashwini Chaudhary 所说,您忘记了这一行末尾的()

letter=input("你要删除哪个字母?").upper

所以letter 不是您所期望的字符串类型,而是builtin_function_or_method(即upper() method of python's string type)。这就是你不能将它连接到另一个字符串的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    相关资源
    最近更新 更多