gpt4 book ai didi

python - 属性错误 : 'tuple' object has no attribute

转载 作者:IT老高 更新时间:2023-10-28 22:18:44 26 4
gpt4 key购买 nike

我是 python 的初学者。我无法理解问题所在?

def list_benefits():

s1 = "More organized code"
s2 = "More readable code"
s3 = "Easier code reuse"
s4 = "Allowing programmers to share and connect code together"
return s1,s2,s3,s4

def build_sentence():

obj=list_benefits()
print obj.s1 + " is a benefit of functions!"
print obj.s2 + " is a benefit of functions!"
print obj.s3 + " is a benefit of functions!"

print build_sentence()

我得到的错误是:

Traceback (most recent call last):
Line 15, in <module>
print build_sentence()
Line 11, in build_sentence
print obj.s1 + " is a benefit of functions!"
AttributeError: 'tuple' object has no attribute 's1'

最佳答案

您返回四个变量 s1,s2,s3,s4 并使用单个变量 obj 接收它们。这就是所谓的tupleobj关联了4个值,s1,s2,s3,s4的值。因此,使用您在列表中使用的索引来按顺序获取您想要的值。

obj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
print obj[3] + " is a benefit of functions!"

关于python - 属性错误 : 'tuple' object has no attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17290114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com