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
1.3k views
in Technique[技术] by (71.8m points)

python - python3 TypeError: 'function' object is not iterable

What change is required in the source code?


    def Update():
        print('
')
        print("Update")
        cmd = os.system('xterm -e apt-get update')
        print("Finish update")

    def AptUpdate():
        print('
')
        print("Update system? {Y/N}")
        print("Y or y")
        print("N or n")
        code = input("Command > ")
        if code == 'y' or code == 'Y':
            for i in Update():
                return Update
            elif code == 'n' or code == 'N': 
                return 
            else: 
                print("Warning!")

    AptUpdate()

    exception:

    Traceback (most recent call last):
      File "pybash.py", line 110, in 
        AptUpdate()
      File "pybash.py", line 102, in AptUpdate
        for i in Update:
    TypeError: 'function' object is not iterable

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

What the traceback error is pointing out is the misuse of for statement:

for i in Updt():

for in python 3 is as follows: "Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence." (source: python 3.3 documentation, section 4: More control structures Python 3

Since a function is neither a list nor a string, you can't use the format:

for [variable] in [function]():

As far as what needs to be fixed, it depends on what those two functions are supposed to accomplish individually.


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

...