AttributeError: 'tuple' 对象没有属性 [英] AttributeError: 'tuple' object has no attribute
本文介绍了AttributeError: 'tuple' 对象没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Python 的初学者.我无法理解问题出在哪里?
I'm a beginner in python. I'm not able to understand what the problem is?
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
接收它们.这就是所谓的 tuple
,obj
与 4 个值相关联,s1,s2,s3,s4
的值.因此,请像在列表中一样使用索引来按顺序获取所需的值.
You return four variables s1,s2,s3,s4 and receive them using a single variable obj
. This is what is called a tuple
, obj
is associated with 4 values, the values of s1,s2,s3,s4
. So, use index as you use in a list to get the value you want, in order.
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!"
这篇关于AttributeError: 'tuple' 对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文