Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

python - AttributeError: 'tuple' object has no attribute

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()

The error I'm getting is:

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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...