想要从列表中删除配对时出现问题

时间:2014-09-21 19:56:01

标签: python list typeerror

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

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'

1 个答案:

答案 0 :(得分:1)

正如the comment of Ashwini Chaudhary所述,您忘记了此行末尾的()

  

letter = input("您要删除哪封信?")。upper

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