有人能帮我解决这个问题吗。我试图寻找答案,但找不到一个
def thAnalysis(direction):
# thAnalysis(<direction>) Time History Analysis Direction in <vertical>, <horizontal>, <both>
both = {
"folder": "VHGM",
"title": "Horizontal + Vertical",
"plot": "Horizontal + Vertical GM plot"}
vertical = { # done
"folder": "VGM",
"title": "Vertical",
"plot": "Vertical GM plot"}
horizontal = {
"folder": "HGM",
"title": "Horizontal",
"plot": "Horizontal GM plot"}
print(direction["folder"])
我将函数称为
thAnalysis('horizontal')
我得到这个错误
print(direction["folder"])
TypeError: string indices must be integers
感谢您对的帮助
在您的示例中,direction
是字符串"水平";,而不是名为CCD_ 3的变量中的字典。不要把字典放在局部变量中,而是把它们放在一个外部dict
中,它会把字符串名称映射到你想要的dict
。
def thAnalysis(direction):
# thAnalysis(<direction>) Time History Analysis Direction in <vertical>, <horizontal>, <both>
directions = {
"both": {
"folder": "VHGM",
"title": "Horizontal + Vertical",
"plot": "Horizontal + Vertical GM plot"},
"vertical": { # done
"folder": "VGM",
"title": "Vertical",
"plot": "Vertical GM plot"},
"horizontal": {
"folder": "HGM",
"title": "Horizontal",
"plot": "Horizontal GM plot"}
}
if direction not in directions:
print("invalid input")
else:
print(directions[direction]["folder"])
thAnalysis("horizontal")