.lower不适用于我在pycharm中的代码

问题描述

我是python的新手,这是我的代码,.lower通常可以使用,但这次却不行。

technical_dict = {
        dict : 'stores a key/value pair',list : 'stores a value at each index',map : 'see dict',set : 'stores unordered unique elements'
    }

    userInput = (input("What term would you like to lookup or type 'exit' to stop:"))
    if userInput.lower() not in technical_dict:
        print("Term does not exist in technical dictionary")
    if userInput.lower()  in technical_dict:
        print(technical_dict[userInput.lower()])
        while userInput.lower() != "exit":
            userInput = input("What term would you like to lookup or type 'exit' to stop:")
            if userInput.lower() in technical_dict:
        print(technical_dict[userInput.lower()])
            if userInput.lower() not in technical_dict:
                print("Term does not exist in technical dictionary")
                break

解决方法

您如何创建字典似乎有问题。您现在拥有的键值不是string或str类型。这就是为什么您会收到字典没有属性“ lower”的错误的原因。要解决此问题,您可以更改值,使其为字符串类型。试试这个:

technical_dict = {
        'dict' : 'stores a key/value pair','list' : 'stores a value at each index','map' : 'see dict','set' : 'stores unordered unique elements'
    }