首页 > 解决方案 > 在Python中调用函数后如何使用方括号

问题描述

我想在调用函数后使用方括号。是这样的

函数()['名称']

我希望输出是“安迪”。def 函数语法应该如何?我总是得到错误,说它不可下标。这是我当前的代码

def function1():
    A={'Name':'Andy'}
    return 

function1()['Name']

标签: pythonlistfunctiondictionarybrackets

解决方案


我不太明白你的问题。但是如果你想使用方括号,并通过字符串查找你的值,你应该使用字典。所以函数和打印应该是这样的``

def function():
    return {'Name':'andy'}
print(function()['Name'])

EDIT: You can do something like this, and that will print the answer, but i dont think its really practicall, because, yo are not storing the name anywhere

def function():
    return {'Name':print('Andy')}

function()['Name']

推荐阅读