Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用3写的字典里的键是str吗?_Python_Dictionary - Fatal编程技术网

Python 用3写的字典里的键是str吗?

Python 用3写的字典里的键是str吗?,python,dictionary,Python,Dictionary,我的算法是,如果我插入一个数字(例如3),那么算法会显示该数字的名称。 但我插入了3,它只打印了“?”,它应该显示学生的姓名和编号 我的代码中有什么问题 dict={1:'윤성준',2:'김희철',3:'강하늘',4:'강철중'} def find_name(dict): a=int(input('students number')) for i in dict: if a==i:

我的算法是,如果我插入一个数字(例如3),那么算法会显示该数字的名称。 但我插入了3,它只打印了“?”,它应该显示学生的姓名和编号 我的代码中有什么问题

    dict={1:'윤성준',2:'김희철',3:'강하늘',4:'강철중'}
    
    def find_name(dict):
        a=int(input('students number'))
        for i in dict:
            if a==i:
                return dict[a]
            else:
                return '?'
输出:

students number3

    ?
students number
3
강하늘

您只需执行查找
my_dict[a]
,无需循环:

my_dict={1:'윤성준',2:'김희철',3:'강하늘',4:'강철중'}

def find_name(dict):
    a=int(input('students number'))
    return my_dict[a]
           
print(find_name(my_dict))

请注意,不应将
dict
用作变量名,因为它是内置关键字。我为您将其更改为my_dict

您只需执行查找
my_dict[a]
,而无需循环:

my_dict={1:'윤성준',2:'김희철',3:'강하늘',4:'강철중'}

def find_name(dict):
    a=int(input('students number'))
    return my_dict[a]
           
print(find_name(my_dict))

请注意,不应将
dict
用作变量名,因为它是内置关键字。我为你把它改成了我的dict

首先,不要使用
dict
它是一个
内置的
名称,你可以对它进行阴影处理。
另外,
Get
方法确保如果找不到密钥,则返回

dic = {1:'윤성준',2:'김희철',3:'강하늘',4:'강철중'}

def find_name(dic):
    a = int(input('students number'))
    return dic.get(a, '?')

print(find_name(dic))
输入(3)/输出:

students number3
강하늘

首先,不要使用
dict
它是一个
内置的
名称,你会给它加上阴影。
另外,
Get
方法确保如果找不到密钥,则返回

dic = {1:'윤성준',2:'김희철',3:'강하늘',4:'강철중'}

def find_name(dic):
    a = int(input('students number'))
    return dic.get(a, '?')

print(find_name(dic))
输入(3)/输出:

students number3
강하늘

这段代码几乎令人满意,但仍有一些改进:

  • 输入是字符串,键是数字,所以使用
    int
  • 在字典中使用
    dict
    以外的其他名称(它是保留关键字)
  • dictionary
    get(key,value\u if\u not\u found)
    方法非常有用,因为您可以搜索dictionary或返回
    value\u if\u not\u found
    if key not found:

    students_to_numbers={1:'윤성준',2:'김희철',3:'강하늘',4:'강철중'}
    
    def find_name(dict):
        a=int(input('students number\n'))
        return students_to_numbers.get(a, '?')
    
    print(find_name(dict))
    
    输出:

    students number3
    
        ?
    
    students number
    3
    강하늘
    

    这段代码几乎令人满意,但仍有一些改进:

  • 输入是字符串,键是数字,所以使用
    int
  • 在字典中使用
    dict
    以外的其他名称(它是保留关键字)
  • dictionary
    get(key,value\u if\u not\u found)
    方法非常有用,因为您可以搜索dictionary或返回
    value\u if\u not\u found
    if key not found:

    students_to_numbers={1:'윤성준',2:'김희철',3:'강하늘',4:'강철중'}
    
    def find_name(dict):
        a=int(input('students number\n'))
        return students_to_numbers.get(a, '?')
    
    print(find_name(dict))
    
    输出:

    students number3
    
        ?
    
    students number
    3
    강하늘
    

    你有一个字典,只是作为
    my_dict[a]
    查找,而不是循环使用
    dict
    作为变量/参数名,你在隐藏内置名。只需使用
    my_dict.get(i)
    。顺便说一句,当条件不满足时,您的代码将立即检查并返回
    “?”
    ,而不是
    继续
    到下一个迭代。按照if-else逻辑,您只需要
    my_dict.get(i,,?)
    您的函数总是在第一个循环中返回一些内容,所以它只测试dict的第一个键。你有一个字典,只需作为
    my_dict[a]
    查找,不循环。不要使用
    dict
    作为变量/参数名,你在隐藏内置名。只需使用
    my_dict.get(i)
    。顺便说一句,当条件不满足时,您的代码将立即检查并返回
    “?”
    ,而不是
    继续
    到下一个迭代。按照if-else逻辑,您只需要
    my_dict.get(i,,?)
    您的函数总是在第一个循环中返回一些内容,因此,它只测试dict的第一个键。在函数外处理输入和输出,而不仅仅是输出,会更好一些。@OlvinRoght,我不确定我是否理解,你想我删除函数声明吗?这只是一个建议。我会将
    input()
    移出函数,并将值作为函数的参数传递。@OlvinRoght啊,我明白了,实际上你建议的方式更简洁,我自己也会这么做,但我在模仿提问者的写作风格,所以我想我现在就保持这样。在函数外处理输入和输出会更好一些,不仅仅是输出。@OlvinRoght,我不确定我是否理解,你想让我删除函数声明吗?这只是一个建议。我会将
    input()
    移出函数,并将值作为函数的参数传递。@OlvinRoght啊,我明白了,实际上你建议的方式更干净,我自己会这样做,但我在模仿提问者的写作风格,所以我想我现在就保持这样。